How to access all the files before compilation? [duplicate] - c

This question already has an answer here:
How do I save preprocessor output using the Dev-C++ IDE?
(1 answer)
Closed 2 years ago.
There are several steps involved from the stage of writing a C program to the stage of getting it executed.
when I compile the code I only get an .exe file. But I want to get all the files which are being made before the compilation (preprocess ones), an intermediate code file
where all those macros with are replaced with their actual values and preprocessor are replaced with their actual header files.
in general can we get all those files (preprocess one, compile one and linker one) separately?

To access all the intermediate files use the command (Ubuntu):
gcc –Wall –save-temps filename.c –o filename. This command generates all the intermediate files in current working directory.

Related

how to compile a program with a given file.a [duplicate]

This question already has answers here:
CMake link to external library
(6 answers)
Closed 1 year ago.
I did a program that need some functions from a file that the staff gives us.
the file is libmap.a.
I developed some modules that use functions from the libmap file.
how can I compile it?
Some friends said me that I need to change something to the Cmake file.
is someone know what I need to change?
You do not compile, only link with the library file.
for gcc you need to use -l option in your case -lmap and if needed set the path using -L option.
in Cmake you need to set your library search path:
LINK_DIRECTORIES(your_target "directory")
and the add the library
TARGET_LINK_LIBRARIES(your_target map)

what do the # numbers in *.i files represent [duplicate]

This question already has answers here:
What is the meaning of lines starting with a hash sign and number like '# 1 "a.c"' in the gcc preprocessor output?
(3 answers)
cpp preprocessor output not able to understand? [duplicate]
(1 answer)
Closed 2 years ago.
I want know about the steps of execution of c program. I got this intermediate file which i can't understand
what are these numbers in the screenshot represent and what exactly that line it do.
# 1 "C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/i686-w64-mingw32/include/stdio.h" 1 3
# 9 "C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/i686-w64-mingw32/include/stdio.h" 3
take a look at ss
.i files are where gcc -save-temps outputs preprocessed C. This is C, not assembly language. You'll find asm in the .s file.
Those are line numbers to match up preprocessed C with the original source. I don't know exactly what the number at the start vs. the one or two numbers after the filename mean. If you need to know the details, you might need to look at GCC internals, or maybe it's documented somewhere.
With nested includes it can get fairly complex, but compilers can use this to print better warning messages (like where the original definition was of a conflicting prototype or something).
GCC has the C preprocessor built-in to the compiler pass so I'm not sure it actually needs to (or can) read these "comment" metadata lines in the .i; in normal operation the main C->asm compiler pass knows file/line-number of everything it read without having to serialize it into this text format and back.
GCC does have options to read preprocessed-C as input, specifically -fpreprocessed which is on by default if you were to run gcc -c foo.i.
Indicate to the preprocessor that the input file has already been preprocessed. This suppresses things like macro expansion, trigraph conversion, escaped newline splicing, and processing of most directives. The preprocessor still recognizes and removes comments, so that you can pass a file preprocessed with -C to the compiler without problems. In this mode the integrated preprocessor is little more than a tokenizer for the front ends.
-fpreprocessed is implicit if the input file has one of the extensions .i, .ii or .mi. These are the extensions that GCC uses for preprocessed files created by -save-temps.
So this is what lets GCC still remove those lines starting with # that are like preprocessor directives. (Like #define or #include).

What are .o files and to open them on windows? [duplicate]

This question already has answers here:
What's an object file in C?
(5 answers)
Closed 3 years ago.
I'm a beginner in programming, I wonder what is inside the .o files and want to see it, but can't open the files in windows because they give some output with unrecognized symbols. Please suggest something !
They are object files, produced by the compiler, which the linker will combine into an executable.
They are not intended to be human readable.

Using C Libraries like OpenGL and LibCurl [duplicate]

This question already has an answer here:
How to Include external C library on windows
(1 answer)
Closed 4 years ago.
I've seen plenty of online tutorials explaining how to use GLFW and libcurl, but where do I actually place the files I downloaded?
For example I downloaded this file for GLFW -
And these are the contents The C+ file wasn't in there I put that there :p
So how would I add the library to any of my .c files?
I've looked everywhere, I might just not be using the right keywords.
And second, how can I have multiple libraries at the same time?
And lastly, what do I put in the <> in the include?
I'm using windows 10 and am using GCC as my compiler. I really should get the Intel one shoudn't I?
This is what the SRC looks like.
In C, a library comes in two parts:
One (or more) header files
The actual library implementation, under Linux either a shared object (.so) or a static library (.a), under Windows I guess it would be a DLL file
You need to include the header file in each C source file of yours where you want to use functionality from the library:
In your source, add a line
#include <library-header.h>
for each header you want to include.
for all header files the library is delivered with.
You will have to tell your compiler where to find the header files. For gcc, you would have to enter something like that on the command line.
gcc -I /path/to/library/directory/include -c my_source.c -o my_source.o
Then, you need to link your program with the actual library. This again, depends on your tool chain, e.g. the compiler you use. For gcc again, the command line would look something like
gcc -lname_of_the_libray -L/path/to/library/directory/ my_source.o -o my_exe
However, libraries are often distributed in source code, which means you would have to compile the library beforehand.

How does my C program know where to find my binary library? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to use dylib in Mac OS X (C++)
I have the following files:
lost.h
lost.dylib
I am struggling with the concept of how my C program knows where my lost.dylib file lives? At the top of my file I do an include #include "lost.h" but how does my program know about my lost.dylib?
At link time it uses a combination of standard directories and user specified directories
Linker options has more information. Basically using -L option you can specify more directories to search for dylib that you are interested in.
If you are using Xcode, this link has more details
As far as runtime finding of the dynamic library is concerned. OS X does it a little bit different that other platforms. OS X embeds an "install name" inside each dynamic library. You can use otool -L to find the details
When linking, you should specify with the linker options (-L for directories, -l for libs). Your library should be named liblost.dylib, and the linker option should be -llost. (strip the 'lib' preffix and the .dylib extension).
Also, when the binary is compiled, you can use the otool command if you want to find out where your executable is searching for the dylib.
And if you want to change that directory, you can use install_name_tool.

Resources