If I downloaded a source tree with a number of folders with source files in them can I use
c_include_path=/some/directory/*
so it searches through all the folders for the files?
AFAIK, this is not possible, you need to provide each directory path separately, most probably using -I option.
The usual convention is, however, to have a top level header at the directory root which includes other required headers with relative path, something like
root.h
#include <this/is/one.h>
#include <that/is/another/one.h>
and, we use #include <root.h> with -I /path/to/rootdir
Related
I am trying to check how include directive in c search linux source tree for including kernel header files. For example in my /usr/src/linux-header-5.7.0-kali-common folder which contains subdirectory include/linux I think this is the directory where avaiable kernel header files like socket.h and others are present and they do exists there. so if in c I try to include /usr/src/linux-header-5.7.0-kali-common/include/linux/socket.h header file like
#include <linux/socket.h>
then it works ok. I checked. but when I try to specify some header file in subdirectory of /usr/src/linux-header-5.7.0-kali-common/include/linux/ like file
/usr/src/linux-header-5.7.0-kali-common/include/linux/amba/bus.h which does exists
#include <linux/amba/bus.h> (NOT working)
and tried to compiled it but gcc gave me error like no such file as <linux/amba/bus.h> exists
I like to know that does gcc not searches any place other place than current directory in linux source tree (may be this is a limitation implemented in some makefile which I dont know of or kernel header files are only self contained folder means no sub directories which in that case I may be wrong from the start because /usr/src/linux-header-5.7.0-kali-common/include/linux/ contains many subdirectories
Question
how can I achieve to include header files that are like #include <linux/netfilter/nfnetlink.h> which does exists in /usr/src/linux-header-5.7.0-kali-common/include/linux/netfilter/ folder
I tried to include it by specifying #include <linux/netfilter/nfnetlink.h> but does not work
Question
if someone try to explain when we include like this
#include <linux/socket.h> (WORKING)
which directory or directories may be searched
I know it is possible to specify #include filepaths either relative to the directory the file is located in, as an absolute file path, or relative to any of the directories in the $PATH system variable. Is there a way to instead specify it relative to the user's current directory when the program is compiled? Let's say I have the following file structure:
|--dir_a/
| |--a.c
| |--a.h
|--dir_b/
| |--b.c
| |--b.h
|--makefile
Now let's say I want to #include the file dir_a/a.h from dir_b/b.h. Using the location of dir_b/b.h, this can be written like this:
#include ../dir_a/a.h
However, this approach has a major flaw in my opinion since it hardcodes the locations of files relative to each other, meaning that relocating a file would require updating the file path everywhere that file was included from.
Using absolute file paths would avoid this problem, but would instead hardcode the location of the project within the filesystem, which seems like bad practice.
Finally, using the <> tags to specify the file path isn't feasible either since I can't assume the project will be listed in the $PATH variable.
So what I want to do is to be able to specify the paths relative to where the user compiles from (or even better, from the location of the makefile). In the above example, this would let me use the following statement to #include dir_a/a.h from dir_b/b.h:
#include dir_a/a.h
This I think would be the ideal solution. It would make the #include statements more consistent and easier to follow, as well as avoid the drawbacks I listed above. Is it possible to do this in any way, eg. with a compiler flag or something? I'm using gcc as my compiler.
If you consistently use <> includes, then the -I options in the makefile should be enough. The directory layout shows only one makefile, in the parent directory. That could use
-Idir_a -Idir_b
in the compiler options, and the .c files could just do
#include <a.h>
#include <b.h>
One of the problems with quoted includes is that their behavior with other compilers may differ, as noted in What is the difference between #include <filename> and #include “filename”? (the standard was not explicit enough). Using a gcc extension probably does not improve that situation.
I managed to solve my problem.
The first part of the solution involves specifying the -iquote flag in gcc when compiling. From man gcc:
-iquotedir
Add the directory dir to the head of the list of directories to be searched for header files only for the case of #include "file"; they are not searched for #include <file>, otherwise just like -I.
The second part of the puzzle was how to get the path to the makefile within the makefile itself. This answer worked for me. I'm pasting the solution here for convenience:
ROOT_DIR = $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
edit: While this approach works, this answer is more cross-compiler friendly, so I'm personally going to use that.
Yes. Any include file, which is not directly in your include path specified in your project linker settings, should have all subfolders up to it specified, like:
#include "first/second/third/folder/library.h"
I'm running through some source code that I was given, but I'm accessing it through SSH. It includes a header file or two which i'm not familiar with and I don't believe is part of the C libraries that are provided.
Is there a way that I can do this? Where should I look in the system files to see what this header file contains?
The top of the file reads:
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include "support.h"
But there is no support.h file in the .c file's directory...where could it be?
You can try find <project-root-dir> -name support.h. It's likely to be in a directory like include, but of course could be anything. Also if it builds, you can look at the compile command and see what -I directories are supplied. As a last resort you can try locate support.h or find / -name support.h.
Apart form searching the filesystem with either find or locate, if you have access to the makefile, you should find the information in the compilation line after in a -I argument.
I have some custom .h files placed under /usr/include, but in a directory (/usr/include/itsmag1c), and I'm trying to include them in my C file. I'm guessing that because I use
#include "filename.h";
for files in the same directory, and I would use angle brackets for including a file like math.h or stdio.h. Am I right in guessing that I would use the angle brackets for including my custom header files? If so, my program wont compile, I get the error that the included files cannot be found. Can someone please point to me how I would include these files, or would it be best to have them in the same directory as my program?
Two choices:
Use #include <itsmagic1c/filename.h>
Use #include <filename.h> as before but add a -I switch.
Boost etc use method 1. (which works well provided you have Boost installed in system locations as you would on a reasonably standard Linux box with reasonable package management).
Method 2. is fine too, but more work on the build system, Makefiles, etc.
Usually, you would put your own headers in the same directory or in a subdirectory. Same-dir includes work with "". For bracket includes, if you use gcc, you can pass additional include directories with
-Irelativedir
or
-I/usr/local/yourpath.
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.