I am studying OOP with c++ and using g++ compiler. I was wondering, when impleme
ID: 3733549 • Letter: I
Question
I am studying OOP with c++ and using g++ compiler. I was wondering, when implementing separate compilation, what is the point of compiling to object files and then linking everything into an executable, instead of just using g++ to compile and link everything directly? What are the pros/cons of each process?
for example: g++ -c main.cpp func1.cpp func2.cpp -o main.o func1.o func2.o
g++ -o test.exe main.o func1.o func2.o
OR simply
g++ main.cpp func1.cpp func2.cpp -o test.exe
they do the same thing it seems, so what is the point of both processes? (I apologize if there are any mistakes that make the question less clear, thank you for your time)
Explanation / Answer
The first way would be a preferable option though both looks almost similar.
Explanation:
--------------
g++ -o test.exe main.o func1.o func2.o
creates binary from the simplified object files.
hence it takes less time compared to the below implementation
g++ main.cpp func1.cpp func2.cpp -o test.exe
as this needs to build binary from .cpp files for which first compiler need to test for syntax and other code compiling errors etc and later generate .exe from those .cpp files.
when we create test.exe for the very first time, the first method takes more time as it first need to generate .o file and later .exe but still this is a suggested technique when we build large projects:
This is because if we change any one .cpp file we just regenerate that .o object file and build .exe by already available .o files and the newly created .o file which takes very less time compared to re generating .exe from all the .cpp files for small change in one cpp file
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.