Is object file generated during single step compilation? - c++

According to How does the compilation/linking process work?
During compilation, the compiler takes the pre-processor's output and
produces an object file from it.
Additionally,
Compilation refers to the processing of source code files (.c, .cc,
or .cpp) and the creation of an 'object' file.
So for example:
If I have 2 files test1.cpp and test2.cpp. I can do modular compilation with g++ -c test1.cpp test2.cpp which generates 2 object files test1.o and test.o.
But what about during single step compilation? Where I have 1 file test1.cpp. With the command g++ test1.cpp, no object file appears to be generated in the directory. So what happens during single step compilation?

This has to do with g++ command line. If you just run it with
g++ file.cpp
It will compile the code to intermediate object file, and than invoke linker and create a.out executable file, removing the intermediate object file after this.

Related

Genetate GCOV report for multiple cpp files

I have 3 files in a folder. one.cpp, one.hpp and main.cpp. one.hpp has a class declaration. one.cpp has definition of functions in the class both under same namespace.Iam calling a function from the class in main and including one.hpp in main. how can compile and run all these files to generate coverage using gcov ?
I tried generating seperate object files and creating an executable .Didn't work
Compiled code using g++ -I. main.cpp one.cpp as John Suggested. To get coverage object after compilation and execution Modified the line as g++ --coverage -I. main.cpp one.cpp.It generated a.out and required .gcna and .gcda files.Generated coverage report as i wanted.Thanks for the help.

Why I cannot run an object file?

When I want to run a source code why this works :
gcc test.c -o test.o
then
./test.o
but this does not work :
gcc -c test.c
then
./test.o
and get this message
bash: ./test.o: Permission denied
First of all, you are not creating an object file but an executable file. Object files are an intermediate file used as input file for the linker to create the executable file. That you name it with an .o suffix doesn't matter.
Secondly, due to tradition if you do not specify an output filename with the -o option the compiler frontend program and linker will create an executable named a.out.
But that's not all, because with the second example you are actually creating a real object file, and those are not executable. Like mentioned above, those needs to be passed to a separate linking step to create the executable file.
You either need to create an executable file:
gcc test.c
./a.out
Or you should link the object file into an executable file:
gcc -c test.c # Create object file
gcc test.o -o test # Use object file to create executable file
./test # Run the executable file
You get that message because the compiler doesn't set the executable bit on object files, because - well, because they are not executable. If you set the executable bit manually and try to run it, you'll get something like "unknown executable format".
Now, it's not just a format problem - the point is that an object file is just half of the work to get something that can actually be executed. In particular, it's missing the linking step, where the linker finds unresolved references and patches them with the addresses from other object files - including the ones you don't specify explicitly, like the standard library - and generates a proper executable file, that the kernel knows how to load and execute.
In the first case you just name the resulting file test.o by using -o, it has been compiled assembled and linked.
In the second case you merely compiled and assemble, it can't run without being linked. See gcc --h or Overall options for gcc for -c:
-c
Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.
By default, the object file name for a source file is made by replacing the suffix ‘.c’, ‘.i’, ‘.s’, etc., with ‘.o’.
Unrecognized input files, not requiring compilation or assembly, are ignored.
(Emphasis mine)
You need to link it and then execute it:
gcc -o a.out test.o
You cannot run an object file. This is not executable and need to be linked to become executable.
Try
gcc -o test test.c and run using ./test.out
This is a fundamental question in gcc. Note that never using parameter -c when you want to get an executable file in single command such as
gcc -c xx.c yy.c -o new
.But you can get an executable file with -c in following commands
gcc -c xx.c yy.c
gcc xx.o yy.o -o new
It's equivalent to
gcc xx.c yy.c -o new

Run C++ executable on Linux

I am trying to compile an execute a C++ program in Linux using OpenCV.
When I type
g++ -c facedetection.cpp -std=c++11 -o facedetection
The facedetection file is correctly generated. Please note that I use -std=c++11 because I had an error advising to do so.
After doing chmod o+x facedetection I try to execute it with ./facedetection but I get error:
bash: ./facedetection: cannot execute binary file: Exec format error
What is wrong?
When you compile with -c, it generates an object (.o) file, not an executable. You need to compile it without -c in order to make an executable file.
Larger C++ programs will have more than one .cpp file; for each .cpp file, you would compile using -c to generate their respective .o file. Then you would link these .o files (running g++ without -c) to generate the final executable.

how to compile c/cpp code in such a way that it gives preprocessed assembled and object file after compilation in linux?

if source file is source.cpp then compiler output should have source.i source.s
source.o in my directory not only .o file.
where
preprocessed = source.i
assembly = source.s
object = source.o
i know first two files are being created but later on they got deleted only .o file is
shown so that linker can link object file but i want to see those two files also.
for linux any flag or something?.
According to the gcc man pages
-save-temps
-save-temps=cwd
Store the usual "temporary" intermediate files permanently; place them in the current directory and name them based on the source file. Thus, compiling foo.c with -c -save-temps would produce files foo.i and foo.s, as well as foo.o. This creates a
preprocessed foo.i output file even though the compiler now normally uses an integrated preprocessor.
so you should compile your code like this
g++ -save-temps source.cpp
You can create individual file for each stage of compiler.
Preprocessor :
g++ -E file.cpp -o file.i
Translator :
g++ -S file.i -o file.s
Assembler :
g++ -c file.s -o file.o
linker :
g++ file.o -o file

Do I need to use -o when I run g++?

I have a very small c++ program that consists of two files: main.cpp and something.cpp. I'd like to compile my program using g++ in the OS X Terminal. Here's what I've tried:
> g++ main.cpp something.cpp
> ./a.out
Is this correct? I've come across examples that use the -o flag. I'm not sure if that's necessary.
Does it work? That's a pretty good indication of correctness! The -o flag allows you to specify a file name so the executable is not named a.out
-o option is not required. If not specified, the default behavior is to put an executable file in "a.out":
-o file
Place output in file file. This applies regardless to whatever sort of output is being produced, whether it be an executable
file, an object file, an assembler
file or preprocessed C code.
If -o is not specified, the default is to put an executable file in a.out, the object file for source.suffix in source.o, its
assembler file in source.s, a
precompiled header file in source.suffix.gch, and all preprocessed C source on standard output.
So yes, this is correct.