-I Flag in GCC ( Linux ) - c

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

Related

Check what files is 'make' including

I'm compiling a kernel module and I'm including <asm/unistd.h>, but I'm not sure if the compiler is using the unistd.h from /usr/includes/ (wrong) or the one from /usr/src/kernel-3.x.x/arch/x86/includes/ (right).
My question is: How can I check which one of those two is the compiler using?
And also, is there a way to force the file from the kernel headers instead of the one from /usr/include?
cpp code.c | grep unistd.h
or
gcc -E code.c | grep unistd.h
To answer the second part of your question:
And also, is there a way to force the file from the kernel headers instead of the one from /usr/include?
You can pass the -nostdinc option to gcc:
"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."
GCC: Options Controlling the Preprocessor

Shared library in /usr/local/lib not found

I don't get it. I usually install third party software into /usr/local so libraries are installed into /usr/local/lib and never had problems linking to these libraries. But now it suddenly no longer works:
$ gcc -lkaytils -o test test.c
/usr/bin/ld.gold.real: error: cannot find -lkaytils
/usr/bin/ld.gold.real: /tmp/ccXwCkYk.o: in function main:test.c(.text+0x15):
error: undefined reference to 'strCreate'
collect2: ld returned 1 exit status
When I add the parameter -L/usr/local/lib than it works but I never had to use this before. Header files in /usr/local/include are found without adding -I/usr/local/include.
I'm using Debian GNU/Linux 6 (Squeeze) which has an entry for /usr/local/lib in /etc/ld.so.conf.d/libc.conf by default and the ldconfig cache knows the library I'm trying to use:
k#vincent:~$ ldconfig -p | grep kaytils
libkaytils.so.0 (libc6,x86-64) => /usr/local/lib/libkaytils.so.0
libkaytils.so (libc6,x86-64) => /usr/local/lib/libkaytils.so
So what the heck is going on here? Where can I check which library paths are searched by gcc by default? Maybe something is wrong there.
gcc -print-search-dirs will tell you what path the compiler checks. /usr/local/lib is simply not among them, so your compile time linker (in this case the new gold ld from binutils) doesn't find the library while the dynamic one (ld-linux.so which reads the cache written by ldconfig) does. Presumably the builds you've done previously added -L/usr/local/lib as necessary in their makefiles (usually done by a ./configure script), or you installed binaries.
This is probably an issue of environment variables - you have something set that's including /usr/local/include but not /usr/local/lib
From the GCC mapage on environment variables
CPATH specifies a list of directories to be searched as if speci‐
fied with -I, but after any paths given with -I options on the com‐
mand line. This environment variable is used regardless of which
language is being preprocessed.
and
The value of LIBRARY_PATH is a colon-separated list of directories,
much like PATH. When configured as a native compiler, GCC tries
the directories thus specified when searching for special linker
files, if it can’t find them using GCC_EXEC_PREFIX. Linking using
GCC also uses these directories when searching for ordinary
libraries for the -l option (but directories specified with -L come
first).
try "printenv" to see what you have set

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

-I dir vs. -isystem dir

If I want to include directories to be searched for header files, which is the preferred way and why?
One way to view this is to use headers that you control with -I and the ones you don't (system, 3rd party libs) with -isystem. The practical difference comes when warnings are enabled in that warnings which come from -isystem headers will be suppressed.
From the gcc documentation for -I:
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.
If a standard system include directory, or a directory specified with -isystem, is also specified with -I, the -I option will be ignored. The directory will still be searched but as a system directory at its normal position in the system include chain. This is to ensure that GCC's procedure to fix buggy system headers and the ordering for the include_next directive are not inadvertently changed. If you really need to change the search order for system directories, use the -nostdinc and/or -isystem options.
So -I is probably the preferred option to specify the location of your header files, except for special cases such as vendor-supplied system headers.
You should use -I to specify the location of your headers.
The files you specify with -isystem are searched after -I is processed and receive a special treatment by gcc (the same as standard system headers).
So here's the difference I've found by running some experiments. Imagine the following setup:
my_std_lib/stdio.h
#ifndef _CUSTOM_STDIO_H
void test() {}
#endif
#include_next <stdio.h>
#include_next <custom.h>
my_user_lib/custom.h
#ifndef _CUSTOM_HEADER_H
void custom_func() {}
#endif
main.cpp
#include "stdio.h"
int main() {
test();
custom_func();
printf("Hello world!");
return 0;
}
If you compile using
g++ -isystem my_std_lib -isystem my_user_lib main.cpp everything will work fine.
However, g++ -isystem my_std_lib -I my_user_lib main.cpp will result into an error
In file included from main.cpp:1:
my_std_lib/stdio.h:10:15: fatal error: 'custom.h' file not found
#include_next <custom.h>
^~~~~~~~~~
1 error generated.
So what is going on?
To my understanding, when I write #include "stdio.h", GCC will start traversing the list of available header files until it finds my_std_lib/stdio.h. Directive #include_next <custom.h> at the end of this file tells the compiler to search for a custom.h by traversing include directories from its current position onwards.
When I add my_user_lib to the list of directories using -I flag, it appears before all the system directories in the directory list. Therefore, it appears in the list before my_std_lib directory and the #include_next fails.
The same would happen if I were to compile using g++ -isystem my_user_lib -isystem my_std_lib main.cpp. Apparently, directories are added to the list in the same order the flags are specified, so, again, my_user_lib will come before my_std_lib.
So in a nutshell, -I and -isystem differ in a way they add their target to the list of include directories.
When you include a header "Myheader.h" using -I, the compiler generates search order: "Myheader.h" , "system/headers". So if something can't be found in "MyHeader.h" you fallback on "system/headers". However when you use -isystem, you are basically saying that replace "system/headers" with whatever I give you. So there is no more falling back on "system/headers".

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