linux kernel source tree in c include directive -- how it works - c

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

Related

How to include a folder of libraries in C?

I'm trying to include a folder that contains a combination of around 60 .h and .hpp files. This folder contains libraries for programming robots with a Wallaby (a mini-computer-like device) for Botball competition. include is located in the same place as main.c (inside code). Up until now, this is what my header for including libraries looks like:
#include "../code/include/accel.h"
Just like accel.h, I have 60 other .h and .hpp files inside include. So, coming to my question, do I need to type out all the 60 header lines? or is there a way to include the include folder.
I'm using Clion for this project, if I can't include the folder itself, does anyone know of a shortcut in Clion to include all the files in include.
I was also thinking of using some sort of placeholder for the folder name and only specify the file type. So, for example: #include "../code/include/(generic placeholder name).h". I have no clue if something like this exists.
I would also request you to keep in mind that I'm a beginner to programming, so please keep your answers simple.
This is just for some extra info:
The Wallaby is a mini computer to which you would connect your sensors, motors, servos and cameras in order to control a robot for the Botball competition. Usually, one can connect to the Wallaby either via Wifi Direct or a cable and write programs on it directly through an online interface (not entirely sure of the word for it, but you just type in an IP address in your browser and it brings up an interface where you can make projects and code). All the code written in that interface saves directly onto the Wallaby. Here the default include statement is #include <kipr/botball.h>, so I'm assuming that botball.h (which is located on the Wallaby's storage) has all those 60 libraries consolidated in it. I got the include folder that I'm using from GitHub. This link was provided to me by one of the Botball organisers. So the main point in me trying to download the library is so that I can write and successfully compile code even when I'm not connected to the Wallaby. Hope this provides some relevant context.
Thank you for your answers!
What I'd do is
Create (maybe with scripting tools or a specific program) a "all.h" file which includes all the other header files
#ifndef ALL_INCLUDED
#define ALL_INCLUDED
#include "accel.h"
#include "bccel.h"
//...
#include "zccel.h"
#endif
Include "all.h" in your main file
#include "../code/include/all.h"
You can create "all.h" automatically every time you build your code.
CLion is an IDE for Clang and GCC. These compilers are instructed to search paths for include files by specifying -I<path> command line arguments. Any number may be specified, and they are searched in the order given, and the first match found is the file that gets included.
I am not familiar with CLion specifically but no doubt it has a dialog somewhere where you can set header file search paths.
Edit: It seems that CLion may not make this so straightforward. I understand that you have to add then via CMake: https://cmake.org/cmake/help/v3.0/command/include_directories.html#command:include_directories, but after that, the IDE will not recognise the header in the editor and will warn you of unrecognised files and will not provide code comprehension features. I believe it will build nonetheless.

#include <x/y.h> works from one project file but not from other? need to change file configurations?

I have a some code files and a directory with some header files in sub-directories, structured like this:
code\my_file.c
code2\other_file.c
headers
where headers contains the sub-directories openssl, curl.
When I use #include <openssl/evp.h> inside my_file.c it fails with:
fatal error: openssl/evp.h: No such file or directory
However, it works from other_file.c
Additionally, when I include the full path #include "../headers/openssl/evp.h it works fine.
Is there a reason one file in my project knows to find the openssl dir and the other doesn't?
Using Eclipse
I tried changing properties, by adding an include path:
but it didn't work...
This what fixed it for me but if someone has another answer, I'd love to hear it
So, it turns out I just needed to do the exact same thing in the GNU C++ tab also:

Including a .h file in a portable ZIPPED folder

I have a question.
My current project has a header I include at the beginning. I include it like so:
#include <C:/Data/Programming/Project_2018/Files/header.h>
This is proving to be a problem as I can't make it portable to another computer. My question is, can I make some sort of change to the #include in order to force the compiler to seek this header.h in the same folder as the c file that uses it?
#include <.../Files/header.h>
With ... representing the folder where the main.c file is contained?
The final destination for this project is a zip folder which is to be delivered to be used on any computer, so I need this portability...
Any clues?
Yes, you can use relative includes in C. If you're compiling from the directory with main.c in it, #include "header.h" will work for you. Note the double quotes, which tell the compiler to look in the source tree, not the include path.
If your directory structure is something like this:
.../files/
.../files/main.c
.../files/include/header.h
then you'll want to #include "include/header.h". You can also move up the tree, so with
.../files/src/main.c
.../files/include/header.h
you can #include "../include/header.h". The path is unix-y, and in unix land, .. is the parent directory.
This question might also be relevant.

as400: C headers include

I'm trying to include another member in a source member.
#include "/QSYS.LIB/MYLIB.LIB/TEST.FILE/HEADER.MBR"
When I compile with crtcmod module(main) srcfile(test) srcmbr(main),
I get an error which says the include file is not found.
If use the command dsplnk obj('QSYS.LIB/MYLIB.LIB/TEST.FILE/HEADER.MBR'),
it does find the file. Why doesn't my C include work?
I must use this file system because it's the only one I have access to.
I found the solution. All I had to do was to use QSYS paths.
#include "HEADER" // if in the same file
or
#include "MYLIB/MYFILE(HEADER)"
For the absolute path include to work, I had to compile from a stream file which compiles from absolute paths. But then, the directive #pragma mapinc that includes my display file wouldn't work anymore because it takes a QSYS path.
The IBM i C and C++ compilers have special code to handle includes. When it finds an include such as
#include <stdio.h>
It will search for this include in one of two ways:
If your source is in a source physical file (using the SRCFILE parameter), it will search through its search path for a file called H with a member named STDIO and include it. The default search path is the QSYSINC library and all the libraries on your library list.
If your source is in the IFS (using the SRCSTMF parameter), it will search through its IFS search path which defaults to /QIBM/include.
To find your own headers, you can do one of two things:
If your source is in a source physical file, you can create a file called H and add your headers as members of that file. Then add the library containing that file to your library list.
If your source is in the IFS, you can put your header in to an IFS directory and add that directory to the INCDIR parameter to CRTBNDC or CRTCMOD.
eg.
CPYF FROMFILE(MYLIB/TEST) TOFILE(MULIB/H) FROMMBR(HEADER) TOMBR(HEADER) CRTFILE(*YES)
ADDLIBLE MYLIB
crtcmod module(main) srcfile(test) srcmbr(main)

eclipse pointing to wrong header file

I am writing some C code in eclipse. I have some of my own header files and I put them under project properties -> GCC C Compiler -> Includes -> Include paths(-l). One of the inclded file is socket.h. When I try to compile my project compiler still points to default library (usr/includes/bits/socket.h and /usr/includes/sys/socket.h) But I have my own socket.h file which I have included under include paths(-l).
My question is, How can I restrict my compiler to point my socket.h rather than its own socket.h ? One restriction is that I cant change socket.h file name.
An easy and fast way to solve this, is to put your socket.h in a subfolder and make your include look like this:
#include "mysubfolder/socket.h"
Add the subfolders parent folder to your include paths and your done.

Resources