Compile c preprocessor output - c-preprocessor

I know you can use cpp directly or call it through gcc -E to generate preprocessor output for an input file.
But how can I, given such an output file, compile it to object code or compile it and link it into an executable image?
Edit:
the whole problem apparently was that I wasn't giving the file that I redirected cpp's output to a .c extension, and gcc didn't like it.

how can I, given such an output file, compile it to object code or compile it and link it into an executable image?
Normally, just like with any other source file, pass it to gcc.
cpp input_file > output_file.c
gcc output_file.c
./a.out

Related

Files accessed by GCC while compiling a given code

I came across the topic of the precompiled headers in C, so I started reading about it, in brief, the article(s) I read said that gcc will use precompiled header (h.gch) if there is one, otherwise normal header file(.h) will be used.
I just wanted to try it out and see if that actually happens with my code. So, I wanna know if there is any command in Linux(Ubuntu) to see what all files are being used by the GCC compiler while it is compiling your code. What I am thinking is, if the .h.gch file is used instead of .h files then it works how it should be and I get the concept of precompiled header files.
For example,
if I do something like
gcc myCode.c
then gcc will definitely go to that file (myCode.c) and if myCode.c file includes a header file then that header file will also be touched/opened by gcc.
https://gcc.gnu.org/onlinedocs/gcc-5.1.0/gcc/Precompiled-Headers.html
This is from where I read about precompiled headers.
If you simply want to see what files are opened by gcc or any other process on Linux then you can use Strace.
strace -f -e open gcc myCode.c

Compile error in c files

I have a c file inside "examples" folder inside ArtoolKIt that I want to compile in Ubuntu.
Artoolkit works perfectly, but how can I compile this c file from terminal?
I try ./helloWorld but it gives me this error:
No such file or directory
Is there any specific way to compile Artoolkit programs cause I don't see it in its docuemntation.
Use either gcc or clang to compile .c files:
gcc -o hello ./examples/filename.c
the parameter following the -o flag names the file that will be produced by the operation, a parameter without any flag named the source input file.
So, the problem that I had was with my directories! For this wo work you should have inside teh folder Examples of ArtoolKit the thiungs: a folder with data, your c file to be compiled and teh makefile in case you have it so youy can compile with $make command.
Now It works for me!

Disable compilation and linking in Code::Blocks?

Programming newbie, I want to disable the compiler/linker and just look at the precompile/ preprocessor's preprocessed code for a program...not sure what this would be called or what the usual method is for doing something like this.
Using the GNU GCC compiler in Code::Blocks, and I looked thru all the various options but not sure the command or what the menu item is called/labeled.
gcc -E source.c -o myfile.i
Here -E is a flag stand's for PRE-Process only.
And -o is another flag which stores the PRE-Processed output of source.c into myfile.i (here .i is common extension given for PRE-Processed files in gcc)
You can use the following option to see the pre-processing files. Normally the compiler will create the files on the fly while trying to create an object file. But at the end removes them.
So in order to view them you can use the command with save-temps.
The output will have the following files:
hello.i-Pre-Processed Output
hello.s-Assembler Output
hello.o-Compiler Output
gcc -save-temps hello.c

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.

Generate assembler code from C file in linux

I would like to know how to generate assembler code from a C program using Unix.
I tried the gcc: gcc -c file.c
I also used firstly cpp and then try as but I'm getting errors.
I'm trying to build an assembler program from 3 different programs
prog1.c prog2.c prog.h
Is it correct to do gcc -S prog1.c prog2.c prog.h?
Seems that is not correct. I don't know if I have to generate the assembler from each of them and then link them
Thanks
According the manual:
`-S'
Stop after the stage of compilation proper; do not assemble. The
output is in the form of an assembler code file for each
non-assembler input file specified.
By default, the assembler file name for a source file is made by
replacing the suffix `.c', `.i', etc., with `.s'.
Input files that don't require compilation are ignored.
so try gcc -S file.c.
From man gcc:
-S Stop after the stage of compilation proper; do not
assemble. The output is an assembler code file for
each non-assembler input file specified.
By default, GCC makes the assembler file name for a
source file by replacing the suffix `.c', `.i',
etc., with `.s'. Use -o to select another name.
GCC ignores any input files that don't require com-
pilation.
If you're using gcc (as it seems) it's gcc -S.
Don't forget to specify the include paths with -I if needed.
gcc -I ../my_includes -S my_file.c
and you'll get my_file.s with the Assembler instructions.
objdump -d also works very nicely, and will give you the assembly listing for the whole binary (exe or shared lib).
This can be a lot clearer than using the compiler generated asm since calls to functions within the same source file can show up not yet resolved to their final locations.
Build your code with -g and you can also add --line and/or --source to the objdump flags.

Resources