Including .h file from other directory - c

Let's say I've got directory a/ with two other directories b/ and c/ in it.
main.c is in b/ but I need to include to it headers.h file which is in c/, is there any way I can go one directory up from b/ to a/ and then include headers.h like this?
#include "c\headers.h"
I want to avoid specyfying the whole path
#include "C:\Program Files\a\c\headers.h"
so that wherever a/ is moved, main.c will still work fine

Most C compilers accept the -I preprocessor option, adding some directory to the include search path.
So configure your build (probably your build automation tool, e.g. your Makefile if you use make) to add such a flag to the compilation command.

Related

gcc include header and get output after preprocessing

I want the preprocessed output of a .c file, but I also want to include a header file without the macro "include..." in the .c file. Usually, you add the -I option for including a directory where headers are.
But if I want to combine -I and -E, gcc does't seem to include my header files in the specified directory.
My command:
gcc -E -I/externDefines myFirmware.c > myFirmware.preprocessed
Does anyone know what the problem could be?
-I does not mean “Include the header files from the given directory in the compilation.” It means “When searching for a file requested with #include, look for the file in the given directory.”
GCC has a command-line switch, -include file that will include a file in the compilation. However, it includes a single file, so you must list each file you want included; it will not automatically include all header files in a single directory. The command-line shell you are using may have features that help generate a list of -include switches with the file names.
A portable way to include a header file X.h while compiling Y.c without changing Y.c would be to create an auxiliary file containing:
#include "X.h"
#include "Y.c"
and then compile that instead of Y.c.

Can Lint resolve the include path of Header Files

I have setup one Project Folder in which i have main Project Directory, say Main_Proj. In that folder, I have created two folders for Header_Files and Source_Files.
Folder Header_Files only contains all header files. lets say module_1a.h and so on. Whereas, folder Source_Files contains main.c and again module wise folders, lets say Module_1 which only contains required .c files of particular module. So for example, folder Module_1 contains Module_1a.c and Module_1b.c.
So the path for Module_1a.c would be =
Main_proj\Souce_Files\Module_1\Module_1a.c
and in all source file I have included the Header Files like below -
//Code for Module_1a.c
#include "..\..\Header_Files\Module_1a.h"
....
My IDE is MP LAB and this code and all modules are working fine with MP LAB xc8 compiler.
Problem occurs when i started Linting my code, its giving me error like -
Error 322: Unable to open include file
'....\Header_Files\Module_1a.h'
I am using PC Lint for C and C++ (version 9). I searched for resolution of this error in Regference manual and got to know to include that directory with -i option.
I also checked with set INCLUDE=<directory Path> but its not working.
Is there any thing i can do with my std.lnt file or do i have to change the folder structure for my Project?
Any source code tree organization where headers or paths contain .. is broken as designed.
The way to go and do away with a lot of problems is
have a single project root directory
Use -I. when compiling, linting, preprocessing, static analyzing, ...
all file references in headers and the project makefile are as seen from the project root
I.e. a header includes other headers using
#include "subdir/whatever/foo_module.h"
and all compilation happens with the working directory being the project root, e.g.
subdir/whatever/foo_module.o: subdir/whatever/foo_module.c
$(CC) $(CFLAGS) -I. -o $# $<
This keeps -I lists extremely short; ideally only -I..
One solution is to use the flag
+fdi // #include search in the directory of the including file
see Error 322 at https://www.kessler.de/prd/gimpel/pclint-meldungen.htm
or
https://www.bezem.de/pdf/htwpl.pdf
or
https://www.gimpel.com/archive/pub90/read90.txt (Section Microsoft's nested #include search)

How To Include Files From Multiple Directories In C on Linux?

gcc main.c -o main -I include
I am creating a small c application with following directory structure:
app=>
=>src (a directory, with all source files)
=>include (a directory, with all header files)
=>common (a directory, with all common files)
=>main.c
Now I am trying to run main.c which contains #include directive to include header files from include directory and function calls to .c files in both common and src directories. I am using -I option but it is useful only for one directory path indication.
How does the compiler will look in all src, common and include directories to resolve the calls.
Kindly suggest me a command or make file to provide path of multiple directories while compiling with gcc.
Multiple -I options are permitted. The description of the -I option from Options for Directory Search
states:
Add the directory dir to the head of the list of directories to be searched for header files. This can be used to override a system header file, substituting your own version, since these directories are searched before the system header file directories. However, you should not use this option to add directories that contain vendor-supplied system header files (use -isystem for that). If you use more than one -I option, the directories are scanned in left-to-right order; the standard system directories come after.
For example:
gcc main.c -o main -Iinclude -Isrc/include -Icommon/include
Note that if main.c is using functions implemented in another .c file(s) then the other .c files will also need compiled and linked into the final program binary. For example:
gcc main.c src/another.c -o main -Iinclude -Isrc/include -Icommon/include

Makefile - include library

I'm trying to write a module that uses the function nanosleep().
when my make file runs it changes the build libary by doing:
make -C /lib/modules/2.6.18-128.4.1.el5/build M=/workspace/lcd-winstar-0.0.1 modules
nanosleep is declare in /usr/include so my makefile cant find it.
what lines should I add to my makefile to include this location as well?????
thanks
-I dir
Add the directory dir to the list of directories to be searched for
header files. Directories named by -I
are searched before the standard
system include directories. If the
directory dir is a standard system
include directory, the option is
ignored to ensure that the default
search order for system directories
and the special treatment of system
headers are not defeated .
&&
-Ldir
Add directory dir to the list of directories to be searched for -l

Header Files in Multiple Directories: Best Practices

I'm a C Newb
I write lots of code in dynamic languages (javascript, python, haskell, etc.), but I'm now learning C for graduate school and I have no idea what I'm doing.
The Problem
Originally I was building all my source in one directory using a makefile, which has worked rather well. However, my project is growing and I would like to split the source into multiple directories (unit tests, utils, core, etc.). For example, my directory tree might look like the following:
.
|-- src
| |-- foo.c
| |-- foo.h
| `-- main.c
`-- test
`-- test_foo.c
test/test_foo.c uses both src/foo.c and src/foo.h. Using makefiles, what is the best/standard way to build this? Preferably, there would be one rule for building the project and one for building the tests.
Note
I know that there are other ways of doing this, including autoconf and other automatic solutions. However, I would like to understand what is happening and be able to write the makefiles from scratch despite its possible impracticality.
Any guidance or tips would be appreciated. Thanks!
[Edit]
So the three solutions given so far are as follows:
Place globally used header files in a parallel include directory
use the path in the #include satement as in #include "../src/foo.h"
use the -I switch to inform the compiler of include locations
So far I like the -I switch solution because it doesn't involve changing source code when directory structure changes.
For test_foo.c you simply need to tell the compiler where the header files can be found. E.g.
gcc -I../src -c test_foo.c
Then the compiler will also look into this directory to find the header files. In test_foo.c you write then:
#include "foo.h"
EDIT:
To link against foo.c, actually against foo.o, you need to mention it in the object file list. I assume you have already the object files, then do after that:
gcc test_foo.o ../src/foo.o -o test
I also rarely use the GNU autotools. Instead, I'll put a single hand-crafted makefile in the root directory.
To get all headers in the source directory, use something like this:
get_headers = $(wildcard $(1)/*.h)
headers := $(call get_headers,src)
Then, you can use the following to make the object-files in the test directory depend on these headers:
test/%.o : test/%.c $(headers)
gcc -std=c99 -pedantic -Wall -Wextra -Werror $(flags) -Isrc -g -c -o $# $<
As you can see, I'm no fan of built-in directives. Also note the -I switch.
Getting a list of object-files for a directory is slightly more complicated:
get_objects = $(patsubst %.c,%.o,$(wildcard $(1)/*.c))
test_objects = $(call get_objects,test)
The following rule would make the objects for your tests:
test : $(test_objects)
The test rule shouldn't just make the object files, but the executables. How to write the rule depends on the structure of your tests: Eg you could create an executable for each .c file or just a single one which tests all.
A common way of doing this is for header files used by a single C file to be named the same as that C file and in the same directory, and for header files used by many C files (especially those used by the whole project) to be in a directory include that is parallel to the C source directory.
Your test file should just include the header files directly using relative paths, like this:
#include "../src/foo.h"

Resources