Eclipse C problems with including .h files - c

I am trying to compile a code. I have the following Structure
RLW/RLW.c (inside a folder)
RLW/RLW.h
main.c
In the main.c if I have the following line
#include "RLW.h"
It does NOT COMPILE and the line has the error RLW.h no such file or directory
if I put the following line in main.c
#include "RLW/RLW.h"
The code COMPILES but there is still the error RLW.h no such file or directory.
I have added the path to the RLW Folder in
Properties->C/C++ General -> Paths and Symbols -> Includes -> GNU C
Any help on how to fix the error?

You have mentioned that inside folder RLW, RLW.h file is available. main.c is present in current working directory(CWD). So to include the folders present in CWD you need to give path as "./folder/xyz.c".Hence giving the include line as #include "./RLW/RLW.h" will solve your problem

Related

Couldn't link source file to main file unless I include source file itself in the main file

Suppose I have 3 files, file.h file.c and fileMain.c. If I just include header file in the main file, it wouldn't link the source file. And gives error saying linker is not correct.
I am using vs-code as an IDE.
What might be an issue and how to fix it ?!
It only works if I add header file and source file both in the main file.
As long as the function name in file.c (definition) matches the name in file.h (declaration), you only need to add #include file.h in fileMain.c.
What it could be happening, is that file.c and file.h are in a different folder or a subfolder relative to fileMain.c (which I assume is the one holding your main function). As an experiment, try moving file.c and file.h to the same folder where fileMain.c is and see if it works.
If it does, it means that you need to tell vs-code where it must find the other files. How to do that will depend on what vs-code is using for compiling. For example, if it is using CMake + Make, you will have a file called CMakeLists.txt where those folders must be referenced.

**.h: No such file or directory when compile C in dec c++

I encounter [Error] interp1_pos2013b.h: No such file or directory when I compile c programs in windows10 dev c++.
File Directory is as follows:
project1: C:\Users\18843\Desktop\新建文件夹
interp1_pos2013b.h:C:\Users\18843\Desktop\新建文件夹\project1\inter1pos
I have added interp1_pos2013b in folder inter1pos and add the folder under project1, As shown in the picture:
enter image description here
I don't know how to solve the problem. Thanks
When you include only a file name, it means, that the file is located near your source file.
In your project, the interp1_pos2013b.h is not in the same folder as velocity_optimize.c.
Write:
#include "inter1pos/interp1_pos2013b.h"
instead
#include "interp1_pos2013b.h"
With this, you tell the compiler the relative path of the header file.

Codeblocks undefined reference to functions error. Already tried add include directory to project build options

I recently inherited a console application that I do not have the project file for. I only have the executable file, the main.c and a bunch of .h header files. I need to edit the main.c and produce an exe file with my edits.
I created a new C console application project on Codeblocks then pasted the main.c contents from the "hand me down" file onto the new project's main.c.
I just edited a fieldname on the main.c file. A very minor edit.
I pasted the .h files to C:\Program Files\CodeBlocks\MinGW\include.
I go to Project >> Build Options >> Search directories >> Compiler >> Add the above include path (saw this step on codeblocks forum and on stackoverflow)
Now the problem is; I'm getting errors, there are multiple undefined reference to 'functionNames'.
These function names are inside the .h files.
I also included the path not just on Compiler but on Linker and Resource compiler to no avail, still getting the same error.
Some of the include .h on main.c also references other .h files which are also pasted on the CodeBlocks\MinGW\include folder.
The include calls are <> and not "" since they are pasted on mingw\include. These .h files are from an api package.
#include <stdio.h>
#include <windows.h>
#include <global.h>
#include <osfile.h>
#include <mail.h>
.. etc
What am I missing? Thanks!

GNU gcc compiler cannot handle partial include statements

Imagine that I have a C-project in the following folder:
C:\microcontroller\stm32\myProject
I have two important folders inside myProject:
- source => here are all my .c and .h files
- build => gcc will write all the object files in here
Note: as you can see, the backward slashes indicate that this is happening on a Windows pc.
The figure below gives an overview:
I will not display my complete makefile here, because that would lead us too far. The rules inside the makefile for all .c => .o files are similar. Let us just focus on the compilation of one specific file: fileA2.c:
--------------------- COMPILATION OF FILE fileA2.c -------------------
Building ./build/folderA/fileA2.o
arm-none-eabi-gcc C:\\microcontroller\\stm32\\myProject\\source\\folderA\\fileA2.c
-o C:\\microcontroller\\stm32\\myProject\\build\\folderA\\fileA2.o
-c
-MMD
-mcpu=cortex-m7
-...
-IC:/microcontroller/stm32/myProject/source/folderA
-IC:/microcontroller/stm32/myProject/source/folderB
Notice that the gcc call ends with two include flags: one for folderA and one for folderB. This enables gcc to use any of the header files from these folders (fileA1.h, fileA2.h or fileB1.h) if fileA2.c has an import statement.
Let us now consider the source code in fileA2.c. We assume that this file needs to include fileA2.h and also fileB1.h.
/*******************************/
/* SOURCE CODE fileA2.c */
/*******************************/
// Some include statements
#include "fileA2.h"
#include "fileB1.h"
// Code
...
These include statements work perfectly. The gcc compiler retrieves the files fileA2.h and fileB1.h in the given folders. But I noticed that the following does not work:
/*******************************/
/* SOURCE CODE fileA2.c */
/*******************************/
// Some include statements
#include "fileA2.h"
#include "folderB/fileB1.h"
// Code
...
The last include statement is a 'partial path' to the file. I get the error when compiling:
fatal error: folderB/fileB1.h: No such file or directory
How can I get gcc to handle this?
PS: It is not my own habit to use 'partial paths'. But they appear a lot in the libraries from the silicon vendor of my chip, so I have to live with it.
You specify two paths to look for includes other than the current directory for the source file:
-IC:/microcontroller/stm32/myProject/source/folderA
-IC:/microcontroller/stm32/myProject/source/folderB
You get the error because neither
C:/microcontroller/stm32/myProject/source/folderA/folderB/fileB1.h nor
C:/microcontroller/stm32/myProject/source/folderB/folderB/fileB1.h exists.
To address the error, you can add the following path:
-IC:/microcontroller/stm32/myProject/source
When using double-quotes to include a header file the compiler first looks in the same directory as the current file. If the header file is not found then it continues with the standard include search paths.
So when the compiler compiles the file source/folderA/fileA2.c the first directory the compiler will look for include files is the source/folderA directory. In the first example the fileB1.h will not be found there, but since you added source/folderB to the standard search path it will be found there as source/folderB/fileB2.h.
In the second example there is no folderB/fileB1.h file on source/folderA so the compiler will search the standard search path. When it comes to source/folderB it will again try folderB/fileB2.h (i.e. source/folderB/folderB/fileB2.h) and it will still not be found, nor will it be found anywhere else.
You need to add -IC:/microcontroller/stm32/myProject/source to be able to find folderB/fileB1.h.
Apart of the two correct responses you have received before this, you have the third chance to specify the path to the file in the #include directive from the curren directory, as with
`#include "../folderB/fileB1.h"

Undefined reference to in AVR-GCC

My main.c is as below
#include <avr/io.h>
#include<avr/interrupt.h>
#include<util/delay.h>
#include <string.h>
#include "main.h"
#include "globle.h"
#include "LCD.h"
int main()
{
...
...
...
lcdInit(0xc0);
lcdScreen(0);
.
.
.
return 0;
}
The definition of lcdInit(0xc0); and lcdScreen(0); is in my lcd.c file
and I have a header file lcd.h having the following lines:
void lcdInit(char);
void lcdScreen(char);
But still I am getting:
C:\Documents and Settings\Tanv\My Documents\my_project5\default/../Main.c:95: >undefined >reference to
`lcdInit'
and
C:\Documents and Settings\Tanvr\My
Documents\my_project5\default/../Main.c:96: undefined reference to
`lcdScreen'
What is wrong here?
This is a linker error.
You are not building your program properly, you need to compile all C files together, like so:
$ gcc-avr -o program main.c lcd.c
or link them together from object files if you compile separately.
Add source and header files to your project by
1. Right click "Source Files" then "Add Existing Source File(s)"
2. Right click "Header Files" then "Add Existing Header File(s)"
Refer to Add Source to Project Step 6.
I had the same problem and I added the files to the project from the beginning and they were compiled together. But this did not solve the problem and I accidentally noticed that I made a mistake, namely the file of the implementation of the functions of the library had the extension .c, and the main extension .cpp. To solve the problem, I simply reassembled the project in c format.

Resources