Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c/c++ : header file not found

Tags:

c++

c

include

Some header files are present in /src/dir1/ (eg: a.h, b.h, c.h etc). My source file is present in /src/dir2/file.cpp. I used some header files that are present in /src/dir1/ but during compilation I got errors like header file not found.

Then I changed the include path like #include "../src/dir1/a.h", then error is gone in file.cpp but I get not found error in the headers files that are present in /src/dir1. Because I included the header file say a.h, that a.h included some other header files that are present in /src/dir1/ (say b.h and c.h present in a.h).

How to add the header file (a.h) in /src/dir2/file.cpp so that it should not ask to modify the include path in the header files that are present in /src/dir1/?

Note: I am using scons to build.

like image 995
NAVEEN Avatar asked Jul 04 '13 16:07

NAVEEN


2 Answers

You can add directories to the include file search path using the -I command line parameter of gcc:

gcc -I/src/dir1 file.cpp
like image 172
Joni Avatar answered Oct 16 '22 04:10

Joni


SCons FAQ:

How do I get SCons to find my #include files?

If your program has #include files in various directories, SCons must somehow be told in which directories it should look for the #include files. You do this by setting the CPPPATH variable to the list of directories that contain .h files that you want to search for:

env = Environment(CPPPATH='inc')
env.Program('foo', 'foo.c') 

SCons will add to the compilation command line(s) the right -I options, or whatever similar options are appropriate for the C or C++ compiler you're using. This makes your SCons-based build configuration portable.

Note specifically that you should not set the include directories directly in the CCFLAGS variable, as you might initially expect:

env = Environment(CCFLAGS='-Iinc')    # THIS IS INCORRECT!
env.Program('foo', 'foo.c') 

This will make the program compile correctly, but SCons will not find the dependencies in the "inc" subdirectory and the program will not be rebuilt if any of those #include files change.

like image 2
LogicG8 Avatar answered Oct 16 '22 03:10

LogicG8