how to modify the path of a .h file when using #include <>? - c

when we use #include < stdio.h > in linux platform, the compiler will search the stdio.h in /usr/include. How to change the path of using #include<>?
thank you.
I asked the question because of this : when I use the C standard function isdigit(), if "#include< ctype.h >" is not added, the program generates a warning but no error. But if "#include < ctype.h >" is added,it will generate an error when linking.(My compiler is not the standard gcc.)
I wonder why?

-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.

There are 2 different ways:
Use -Idir in the Makefile or as an argument to gcc.
Create environment variable C_INCLUDE_PATH (for C header files) or CPLUS_INCLUDE_PATH (for C++ header files).

Related

Installing a C header on Linux/POSIX systems

I have a header foo.h with functions bar(), baz(), qux(). Where would I copy it/what would I have to do it so that I can include it in C programs like other systemwide headers, like stdio.h, unistd.h etc?
From the GCC documentation (I am assuming you are using GCC since you included the Linux tag):
2.3 Search Path
GCC looks in several different places for headers. On a normal Unix system, if you do not instruct it otherwise, it will look for headers requested with #include in:
/usr/local/include
libdir/gcc/target/version/include
/usr/target/include
/usr/include
[...] In the above, target is the canonical name of the system GCC was configured to compile code for; often but not always the same as the canonical name of the system it runs on. version is the version of GCC in use.
So that mostly answers your question. But really, you probably shouldn't be putting non-system headers in places like /usr/include. Most of the time, it's best to keep the headers for your program in the include sub-directory for the project. Then tell GCC how to find those files like this:
You can add to this list with the -Idir command line option. All the directories named by -I are searched, in left-to-right order, before the default directories. The only exception is when dir is already searched by default. In this case, the option is ignored and the search order for system directories remains unchanged.
[...]
Also keep in mind the differences between #include "file.h" and #include <file.h>
GCC looks for headers requested with #include "file" first in the directory containing the current file, then in the directories as specified by -iquote options, then in the same places it would have looked for a header requested with angle brackets. For example, if /usr/include/sys/stat.h contains #include "types.h", GCC looks for types.h first in /usr/include/sys, then in its usual search path.
[...]

Preprocessor concatenation for include path

I have a set of includes that reside in a far off directory meaning that including them requires a long include, such as:
#include "../../Path/to/my/file.h"
Where I have multiple of these it becomes a bit inconvenient so I am thinking I may be able to use a #define for the directory path and then concat the file name that I need, i.e.
#define DIR "../../Path/to/my/"
#define FILE1 "file.h"
#define FILE2 "anotherFile.h"
#include DIR FILE1 // should end up same as line in first example after pre-proc
However this does not work... is there anyway to concatenate within the workings of the C pre-processor suitable for this?
The compiler will do macro replacement on an #include line (per C 2011 [N1570] 6.10.2 4), but the semantics are not fully defined and cannot be used to concatenate file path components without additional assistance from the C implementation. So about all this allows you to do is some simple substitution that provides a complete path, such as:
#define MyPath "../../path/to/my/file.h"
#include MyPath
What you can do with most compilers and operating systems is:
Tell the compiler what directories to search for included files (as with GCC’s -I switch).
Create symbolic links to other directories, so that #include "FancyStuff/file.h" becomes equivalent to ../../path/to/FancyStuff because there is a symbolic link named FancyStuff that points to the longer path.
You can't customise the search path for include files like this, but you can tell the compiler where to look for include files. Many compilers -I option for that, e.g.:
gcc -c stuff.c -I/path/to/my/ -I/path/to/other/
If that makes your compilation command too long, you should write a Makefile or, if you are working in Visual Studio or similar IDE, customise the search path in your project settings.

Compiling a C prog winth unix syle header files in windows

well i have a few Cpp source and header files, and the header files have include statements in the form,
#include<include/config.h>
#include<include/controls.h>
the thing is im using gcc on windows and it says no such file or directory as the windows style paths has '/' and not '\' ,
so i changed the path to include\config.h but again, the problem is config.h has many header files included in it with the similar unix path style, and its not feasible to change the paths in all the header files cos its a library and there are 100s of such headers, is there any way to compile this using GCC (minGW) ??
Thanks :)
this may sound like a silly problem, sorry if it is!!..
I don't think the direction of the / is the problem here. Windows should convert between the two for you when calling its API precisely for the purposes of (some) unix compatibility.
I think the problem is the include path. Try compiling your program with
gcc -o output.exe -I"c:\path\to\directory\above\include" file.c
So that in the directory you specify with the include flag, there is a subdirectory "include" containing your headers. This assumes all your paths in your other headers are relative to this.
config.h and controls.h are not standard header files. Try this instead:
#include "include/config.h"
#include "include/controls.h"
Even better would be to use the command line to specify the include directory and use
#include "config.h"
#include "controls.h"
Probably mingw uses the same option as all other c compilers: (compiler name) -I(directory name)...
As others have stated, / vs. \ is a non-issue. Even Microsoft compilers and utilities accept / everywhere. The use of \ is a historical mistake, perpetrated needlessly.
A good discussion of your issue can be seen here:
How to generate a OS independent path in c++
Don't change forward slash / to back-slash \ - you are making the compiler to interpret the next character as a special character \c. GCC has no problem dealing with UNIX-style paths on Windows. The problem is probably the lack of -I directive to the compiler - something like -I. to search for files in current and sub-directories.

How to link a non-standard header file into a C compiler

I'm trying to use a non-standard header file (http://ndevilla.free.fr/gnuplot). Its used in lots of codes in various different places on my computer. Currently I have to put the header file and the object file in every folder which its needed with the preprocessor directive:
#include "gnuplot_i.h"
In the file. Is there a way by which I can put the header file in one place so I can reference it like other standard header file. Cheers.
Compile with -I<directory>
E.g.
compile with -I/usr/local/gnuplot/inc.
Also it might be worth your reading up on include paths and the difference between:
#include <include_file.h>
and
#include "include_file.h"
Linking in an object file needs to be done explicitly the same way as a C file, which means (I believe) that you need a full path. However if you archive it into a proper library then you can use -l<library name> and -L<library path> instead. E.g.
gcc -I/usr/local/gnuplot/inc -L/usr/local/gnuplot/lib -lgnuplot -o my_prog my_prog.c
Most compilers have a flag -I that lets you add a directory of your choosing to the search path for include files.

What is the difference between <stdio.h> and "stdio.h"? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
what is the difference between #include <filename> and #include “filename”
In both cases there is no error ...Is there any difference between them ?
<stdio.h> searches in standard C library locations, whereas "stdio.h" searches in the current directory as well.
Ideally, you would use <...> for standard C libraries and "..." for libraries that you write and are present in the current directory.
The second version is specified to search first in an implementation defined location, and afterwards if the file is not found, search in the same place as the <...> version, which searches in the paths usually specified by the -I command line option and by built-in include paths (pointing to the location of the standard library and system headers).
Usually, implementations define that location to be relative to the location of the including file.
The <> tell the compiler to look for the file in the libraries' headers and "" tell it to look around among your application's headers.
As for why both of them works for you, maybe your compiler also looks for the filename in the library headers in case it didn't find one among yours.
You use #include when you want to say: "look for a file with this name in the system's include directory". You use #include "doublequoted" when you want to say: "look for a file with this name in my own application's include directory; however, if it can't be found, look in the system's include directory".
#include <something.h> is meant for system headers, while #include "something.h" is for headers of your own program. System headers are searched for in usual system directories (and those included with -I argument), which your headers are searched for in current directory and then the same locations as system headers.
see http://gcc.gnu.org/onlinedocs/gcc-2.95.3/cpp_1.html#SEC6
For the compilers I've used, "..." starts looking for the include file in the same directory as the source file that is being compiled, then the include path. Includes with <...> start in the include path, skipping the current die unless it is in the include path.
Normally standard header files are enclosed by < > and other user specific files are specifed with " .
I case of "..." compiler first search the header file in your local directory where
your .c file presents
while in case of <...> compiler only search in header file folder
The difference is that header files made by the developer are enclosed by "". Header files that are already in the system are enclosed with <>. Even the <> headers need the -I directive if the directories that are placed are not in the search path of the compiler.
Bottom line: Your headers with "", system headers with <>
<stdio.h> refers to a header (not a header file)
"stdio.h" refers to a source file.
Headers need not exist phisically in the implementation; the way they are identified is implementation-defined (usually the headers are files on specific directories)
When the directive uses ", the source file is searched in an implementation-defined manner and, if not found, the directive is reprocessed as if it was written with < and > in the first place.

Resources