Makefile - include library - c

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

Related

Including .h file from other directory

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.

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)

-I Flag in GCC ( Linux )

I Have found a source file bundle with a Makefile, I went through it, and In CFLAG Variable, There is a FLAG -I , I have searched on the web, But couldn't find what it actually does. Is it something relevent to the library files included in the C file? ( stdio.h,unistd.h, pthread.h )
Please point me to a source or explain in brief, What does the Flag -I does?
-Regards
It's right there in the man page of gcc (called with man gcc on unix/linux or you can find it via Google):
-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 . If dir begins with "=", then the "=" will be replaced by the sysroot prefix; see --sysroot and -isysroot.
(The exact text and semantics may differ between versions of gcc)
Alternatively there is also the gcc documentation online: http://gcc.gnu.org/onlinedocs/ The option for version 4.8.2, as an example, can be found here: http://gcc.gnu.org/onlinedocs/gcc-4.8.2/gcc/Directory-Options.html#Directory-Options

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

Manipulating the search path for include files

My development environment is such that I have some_header.h in /usr/include and in /another/directory. /another/directory contains some header files I need to include in my program, but I want to use some_header.h from /usr/include. When I use
gcc ... -I/another/directory
gcc uses /another/directory/some_header.h. If I use
gcc ... -I/usr/include -I/another/directory
gcc does the same thing because it ignores /usr/include since it is part of the standard search path, but it gets searched after non standard directories included with -I.
Any ideas?
Use the -iquote switch:
Include the files that are in another/directory using quotes:
#include "another_file.h"
Then use
gcc -iquote /another/include ...
to add a search path for quoted include files. This switch will add a directory that is searched for quoted include files after the current directory and before -I and system include paths.
Include your other include files using brackets (i.e. #include <header.h>).
See here for more information:
Where are include files stored - Ubuntu Linux, GCC
Have you looked at -nostdinc ?
The manual says:
-nostdinc
Do not search the standard system directories for header files.
Only the directories you have specified with -I options (and the
directory of the current file, if appropriate) are searched.
Of course that means that you will have to specify anything that normally goes on the standard search path that you do want...
Have you tried unsetting the system INCLUDE path environment variable?

Resources