Question:

C++ Compiler Question?

by Guest62202  |  earlier

0 LIKES UnLike

Ok so I have a bit of experience in C++ (Just a High School programming class last year) and I recently decided to get back into it. So I found an E-book online and it was talking about how to compile to an executable and what it said was that once you have your source file you then compile it to an object file (.obj) then link separate object files to get your executable

So the build process is like this:

.cpp -> .obj -> .exe

However I was always taught that you compile your source directly to an executable removing the step where you link these object files.

Making the steps to build to an executable like this:

.cpp -> .exe

Can anyone elaborate on this step of linking object files to make an executable for me? I was using Dev-Cpp to compile my source code last year so maybe Dev-Cpp automatically does this for you and the E-Book I am reading is just speaking of different compilers?

 Tags:

   Report

1 ANSWERS


  1. The shortcut of compiling source directly to an executable is fine, for simple programs. Here's an example of why it can be useful to compile to an object file before linking:

    Suppose you wrote a utility class that can be used in a variety of applications. Let's say the class manages a variable length array of bits. We'll call it BitArray. First you'd compile BitArray to an object file:

    g++ -c BitArray.cpp

    Now you have BitArray.o that you can link in wherever you need it:

    g++ -o testProg1 testProg1.cpp BitArray.o

    g++ -o testProg2 testProg2.cpp BitArray.o

    You only need to compile BitArray once, thus saving compile time when you build other programs.

    This is a very simple example, but you can see how this can be extended to larger applications. You may want to collect together many useful utilities, several .o files, into a single archive, e.g. UtilLib.a, that would make linking easy for many programs that need one or more of your utilities.

    How easy it is to manage your compiling and linking depends on your development environment.

You're reading: C++ Compiler Question?

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions