Header files linked to from header file not found. - c

I have a problem with Nvidia's OpenCl/Cuda framework, but I think it is a gcc linking issue.
The opencl_hello_world.c example file uses following header file:
#include "../OpenCL/common/inc/CL/opencl.h"
with opencl.h using these header files:
#include <../OpenCL/common/inc/CL/cl.h>
#include <../OpenCL/common/inc/CL/cl_gl.h>
#include <../OpenCL/common/inc/CL/cl_gl_ext.h>
#include <../OpenCL/common/inc/CL/cl_ext.h>
So all the header files are in the same folder.
When I then compile with gcc opencl_hello_world.c -std=c99 -lOpenCL I get following error messages:
error: ../OpenCL/common/inc/CL/cl.h: No such file or directory
error: ../OpenCL/common/inc/CL/cl_gl.h: No such file or directory
...
Even though cl.h and the other header files are located in this folder.
Having searched SO, I then changed the includes in the opencl.h to
#include "cl.h"
#include "cl_gl.h"
how I have read here: gcc Can't Find a Included Header.
But messing around with the frameworks header files does not seem like the way to go? What would be the proper way to handle this problem?

You're using both #include "" form and #include <>, which don't search in the same paths. "" is local to your project, and the -i command line specified to gcc, <> is the 'system' path specified by -I to gcc.
You probably need to set the include path with -Ipath/to/includes in gcc's command line.

Related

Windows Command line c file compile error

I work with C codes using gcc compiler and Clion IDE
So, I have a src folder for .c codes and include folder of .h files which contains a bunch of #define
I include some of the .h files in my main.c programe
when I run it in my Clion project, everything work fine
but when I try to run with command line (cuz I need command arguments), it says
Fatal Error: MyFunction.h, no such file or directory MyFunction.h IS MY FILE, NOT DEFAULT library file
What I typed in the command line is $gcc C:\Users\Admin\CLionProjects\project\src\main.c
What I get is "C:\Users\Admin\CLionProjects\project\src\main.c 10: Fatal Error: MyFunction.h No such file or directory.
Then I try to change the #include <MyFunction.h> to #include "MyFunction.h", hoping it will solve the problem, but no.
Then I did a test, I call a function of another .c file in the same src folder, and use command line to run main.c, but it tells me the function is not even defined.
cmd can recognized all the default library files like stdio.h, but none of those created by me. Any idea how to solve this problem? I know it must be some kind of path error
You have to tell gcc preprocessor where to find files you want to include.
You have two kinds of header:
System headers: included with #include <header>.
Local headers: included with #include "header".
Preprocessor searchs in header search path to find system headers.
Preprocessor searchs in current directory, then in header path to find local headers.
You can add folders in header search path using -I option in gcc invocation.
So in your case, you can compile with these commands:
$gcc -I C:\Users\Admin\CLionProjects\project\include C:\Users\Admin\CLionProjects\project\src\main.c
or
$cd C:\Users\Admin\CLionProjects\project\src
$gcc -I ..\include main.c

Is there a way to ask gcc to treat #include <> like #include ""?

Is there a compiler or preprocessor flag that will force gcc to treat #include <x.h> like it would #include "x.h"? I have a bunch of generated code that uses #include <> for files in the current directory, and gcc reports No such file or directory for these files. I'm looking for a workaround that doesn't involve editing the code.
EDIT: -I. doesn't do it. Let's say I have the following files:
foo/foo.h:
#include <foo2.h>
foo/foo2.h:
#define FOO 12345
xyz/xyz.c
#include <stdio.h>
#include "foo/foo2.h"
int main(void)
{
printf("FOO is %d\n", FOO);
return 0;
}
If, inside the xyz directory, I compile with gcc -o xyz I.. xyz.c, the compile fails:
In file included from xyz.c:2:
../foo/foo.h:1:18: error: foo2.h: No such file or directory
xyz.c: In function ‘main’:
xyz.c:6: error: ‘FOO’ undeclared (first use in this function)
xyz.c:6: error: (Each undeclared identifier is reported only once
xyz.c:6: error: for each function it appears in.)
Adding -I. doesn't change anything.
But, if I change foo/foo.h to:
#include "foo2.h"
Then the compile works. I know I could add -I../foo to my command line, but I was looking for a more generic way to treat #include <> as #include "". Does one exist?
Yes, you can pass the switch -I . to the compiler to add the current directory to the include search path.
The -I- option might help you. From gcc's man page:
-I- Split the include path. Any directories specified with -I options
before -I- are searched only for headers requested with
"#include "file""; they are not searched for "#include <file>". If
additional directories are specified with -I options after the -I-,
those directories are searched for all #include directives.

#include "existing file" fails: no such file (C)

Compiling C with gcc.
While
#include "/absolute/path/to/my/file"
works OK,
#include "../../relative/path/to/my/file"
fails with "no such file or directory". This only happens when the file is placed outside the project directory. file has read permissions. What could be the reason?
When using the format
#include "some_file.h"
the preprocessor by default looks in the same directory as the source file, if the file is not found there, it looks in the header-file search paths.
If the header file is not in the same directory as the source file, and not in one of the directories of the preprocessors search-path, then it will not be found.
You can write relative or full paths though:
#include "../some_directory/some_file.h"
Yes, you need GCC Options for Directory Search
When using gcc and local header files you need to add an include path to your build command.
mysource.c:
#include "localfile.h"
build command:
gcc -o program mysource.c
This works as long as the header file is in the same directory as your source (where you're running the command). If your header file is in a different directory you can include with the -I option:
gcc -I../headerdir -o hello.exe hello.c
or an absoulte path:
gcc -I/home/user/myprogra/headerdir -o hello.exe hello.c

Search Path for C Code Project

I have a third party Curl project, inside there is a lib folder containing the source file,
and also a include folder, inside include folder there is a curl folder which a bunch of .h files
/lib/***.c and /lib/***.h files
/include/curl/curl.h
Inside the sources files of the /lib there are many calls to
#include <curl/curl.h>
I copy the /lib and /curl folder into my new projects. Now When I compiled there's an error on
Lexical or preprocessor error on #include <curl/curl.h>
I added the Header search path and User search path
"$(SRCROOT)/curl"
which points to /curl folder containing curl.h file.
but the project still has compile issue on
ANd I don't want to change that to just because there are too many occurrences. ANy ideas?
Use -I compilation flag to specify the include path.
e.g.
gcc -I/var/lib curl.c
If the #include is #include <curl/curl.h> then the header file will be found in directory /var/lib/curl.
Try This
gcc curl.c -I/(PATH TO HEADER FILE)
if you have any shared libraries to link then try this
gcc curl.c -I/(PATH TO HEADER FILE) -L/(path to lib)

Make / Build with geany

I have problem on build.
My error is something like that: "test.c" error: test.h error"
I have on source code:
#include <test.h>
How to add this on build?
To be spesific How to link this library? Can i link it through Geany?
Operating System ubuntu + Geany as compile
Follow these simple steps:
Create a directory and place your test.c and test.h in the same directory.
In C source file you should write:
#include "test.h" which will search for the header file in current directory as well as the include library path, instead of, the #include <test.h> which will search for test.h in include libraries path only.
Build as usual, using geany.

Resources