as400: C headers include - c

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)

Related

gcc 5.3.0 for unix reports included c file not found despite it existing

I created a very simple hello world program in C and I'm trying to call a function from an included file. The problem is the gcc program is strange in how it recognizes files.
Suppose the main file is named a.c and the file I want to include is b.c
So in my headers, I add:
#include <./b.c>
and when I run gcc 5.3.0, I receive this error:
./a.c:xx:yy: fatal error: ./b.c: No such file or directory
compilation terminated.
(where xx is the line number of the include line and yy is where the > is located on that line.)
I also tried another idea. I executed gcc while including the library path as follows:
gcc -I./ a.c
and I still get the same error.
In all tests, the file b.c exists in the same folder as a.c is and that I execute gcc from.
Now If I change the include line in the source to the following:
#include </path/to/b.c>
(thereby replacing the relative path with an absolute path) and run gcc, the file is then read for processing.
Is there a way I can change my code so I don't have to constantly specify absolute paths if I need to include multiple custom C files that are all in the same folder as the code that references them?
Like... is there such thing as....
#setlibpath /path/to/my/c/files
#include <./item.c>
#include <./item2.c>
....
#include <./itemn.c>
or do I have to do this....
#include </path/to/my/c/files/item1.c>
#include </path/to/my/c/files/item2.c>
....
#include </path/to/my/c/files/itemn.c>
Converting my comments into an answer.
Use double quotes around the name and the header (source file) will be found. Use angle brackets around system headers. And generally, avoid using ./ (and ../ even more so) in header names. For GCC, see also Include Operation and the following sections.
Also see the POSIX specification for the c99 compiler for the -I option:
Change the algorithm for searching for headers whose names are not absolute pathnames to look in the directory named by the directory pathname before looking in the usual places. Thus, headers whose names are enclosed in double-quotes ("") shall be searched for first in the directory of the file with the #include line, then in directories named in -I options, and last in the usual places. For headers whose names are enclosed in angle brackets (<>), the header shall be searched for only in directories named in -I options and then in the usual places. Directories named in -I options shall be searched in the order specified. If the -I option is used to specify a directory that is one of the usual places searched by default, the results are unspecified.
Note that ./b.c is not an absolute pathname — so the name is appended to a set of directories, and the current directory is not used unless the #include line uses double quotes.

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

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

How to include custom .h files in /usr/include

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.

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