gcc include header and get output after preprocessing - c

I want the preprocessed output of a .c file, but I also want to include a header file without the macro "include..." in the .c file. Usually, you add the -I option for including a directory where headers are.
But if I want to combine -I and -E, gcc does't seem to include my header files in the specified directory.
My command:
gcc -E -I/externDefines myFirmware.c > myFirmware.preprocessed
Does anyone know what the problem could be?

-I does not mean “Include the header files from the given directory in the compilation.” It means “When searching for a file requested with #include, look for the file in the given directory.”
GCC has a command-line switch, -include file that will include a file in the compilation. However, it includes a single file, so you must list each file you want included; it will not automatically include all header files in a single directory. The command-line shell you are using may have features that help generate a list of -include switches with the file names.
A portable way to include a header file X.h while compiling Y.c without changing Y.c would be to create an auxiliary file containing:
#include "X.h"
#include "Y.c"
and then compile that instead of Y.c.

Related

C and MinGW: How do I fix my "No such file or directory" error?

I have made a python "compiler" that helps me compile my C code with gcc, for example it fetches all my header files and source files. So my cmd commmand is gcc {headers} {source} -o {build_dir}/build.exe -lgdi32 -w where {headers} is a string like -Ipath/to/headers/foo.h -Ipath/to/other/headers.foo2.h and where {source} is the same but with .c files. It seems that the compiler finds the header files, but when compiling my code it fails.
(btw I am trying to make a portable programming environment on my flash drive so python and mingw are both portable)
This is the error: fatal error: test.h: No such file or directory #include "test.h"
My project tree
I have put the third party library files into the mingw directory instead of making a custom one and then linking it in the gcc command.
The -I option takes the path to the directory containing the header files or more specifically with an argument -Ipath and a directive #include<a/b.h>, the compiler will try to look for the header file at path/a/b.h.
So you should not give it paths to header files, only to the directory or directories relative to which you use include directives.

Windows Command line c file compile error

I work with C codes using gcc compiler and Clion IDE
So, I have a src folder for .c codes and include folder of .h files which contains a bunch of #define
I include some of the .h files in my main.c programe
when I run it in my Clion project, everything work fine
but when I try to run with command line (cuz I need command arguments), it says
Fatal Error: MyFunction.h, no such file or directory MyFunction.h IS MY FILE, NOT DEFAULT library file
What I typed in the command line is $gcc C:\Users\Admin\CLionProjects\project\src\main.c
What I get is "C:\Users\Admin\CLionProjects\project\src\main.c 10: Fatal Error: MyFunction.h No such file or directory.
Then I try to change the #include <MyFunction.h> to #include "MyFunction.h", hoping it will solve the problem, but no.
Then I did a test, I call a function of another .c file in the same src folder, and use command line to run main.c, but it tells me the function is not even defined.
cmd can recognized all the default library files like stdio.h, but none of those created by me. Any idea how to solve this problem? I know it must be some kind of path error
You have to tell gcc preprocessor where to find files you want to include.
You have two kinds of header:
System headers: included with #include <header>.
Local headers: included with #include "header".
Preprocessor searchs in header search path to find system headers.
Preprocessor searchs in current directory, then in header path to find local headers.
You can add folders in header search path using -I option in gcc invocation.
So in your case, you can compile with these commands:
$gcc -I C:\Users\Admin\CLionProjects\project\include C:\Users\Admin\CLionProjects\project\src\main.c
or
$cd C:\Users\Admin\CLionProjects\project\src
$gcc -I ..\include main.c

How To Include Files From Multiple Directories In C on Linux?

gcc main.c -o main -I include
I am creating a small c application with following directory structure:
app=>
=>src (a directory, with all source files)
=>include (a directory, with all header files)
=>common (a directory, with all common files)
=>main.c
Now I am trying to run main.c which contains #include directive to include header files from include directory and function calls to .c files in both common and src directories. I am using -I option but it is useful only for one directory path indication.
How does the compiler will look in all src, common and include directories to resolve the calls.
Kindly suggest me a command or make file to provide path of multiple directories while compiling with gcc.
Multiple -I options are permitted. The description of the -I option from Options for Directory Search
states:
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.
For example:
gcc main.c -o main -Iinclude -Isrc/include -Icommon/include
Note that if main.c is using functions implemented in another .c file(s) then the other .c files will also need compiled and linked into the final program binary. For example:
gcc main.c src/another.c -o main -Iinclude -Isrc/include -Icommon/include

How to get the absolute path of specific header file?

I have a C file with some #include instructions of header files. I know that when I compile the C file with gcc, the compiler looks for the .h files in the paths contained in the environment variable $CPATH. How can I get the absolute path of a specific .h file? (I prefer to get the path without compilation)
If you compile with -E you will get the preprocessor output and within that all of the included files will have their full paths. eg in the following, the file 'file' will contain the preprocessed output:
gcc -E -o file file.c
This is a bit of a hack:
cpp -MD file.c | sed -n '/\.h"/s/.*"\(.*\)".*/\1/p' | sort -u
and clearly depends on the output format of cpp. (The above works for gnu cpp 3.4.6)
This gives the full paths of all included files. Fun it through another filter to reduce the output to the specific header you care about.

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