1

EDIT: FOR FUTURE VIEWERS -- See Ken White's comment for an answer. (As it turns out, the premise of my question was slightly flawed, however, this exchange may help future students as it did me.)

I have a program consisting of several cpp and several header files. I would like to understand what happens after the linker links all of these files, and preferably find a way to view the project as if it were already linked together.

Browsing through the project directory right now, I see .o files, which I know are object files. I see the individual .cpp and .h files as well as a .exe. But, to my dismay, I see no file which shows the entirety of the program. Is there one massive .cpp file that I can view? My understanding is that this file amalgamation is what happens after the linking occurs anyway, right before the compiling step happens.

Here is an analogous series of files (and the files they include), which, together make a whole program:

file: main.cpp

    #include "classA.h"
    #include "classB.h"

file: classA.cpp

    #include "classA.h"

file: classB.cpp

    #include "classB.h"

file: classA.h

file: classB.h

But what I want to view is how the code would look if at had been linked already, something like:

file: mysterious.cpp (which would contain the contents of:)

    classA.h
    classA.cpp
    classB.h
    classB.cpp
    main.cpp

But altogether, with line numbers.

For context, I came up with this question while using valgrind to detect memory leaks and noting that errors seemed to be occurring at line numbers in my program which seemed too large to exist in any of the individual files.

--an inquisitive student

  • 4
    There is no massive *file amalgamation*. The source files are compiled into object files, and then the linker takes those object files and combines them (along with code from referenced libraries) to produce the executable file. The linker works with binary files, not source code. And you've got the process backward - the code is compiled, and then the resulting object files are linked. There is no step between *linking and compiling*, because the compiling is done first. – Ken White Jun 21 '19 at 01:35
  • 1
    The point of linking is the massive linked file is actually the .exe itself. The process goes preprocessing -> compiling -> linking. The result of each step is some file in memory -> object file -> exe. –  Jun 21 '19 at 02:17
  • 1
    There is such a thing as a “unity build”, but that’s not the default approach. – Davis Herring Jun 21 '19 at 03:41

0 Answers0