0

I am reading a book called jumping into C++ and there is a part of the book that says this:

For example, a statement such as

#include <iostream>

tells the preprocessor to grab the text of the file iostream directly into the current file. Every time you include a header file it will literally be pasted into the file before the compiler sees it, and the #include directive will be removed.

So from this I understood that all of the files are pasted into one file so you are left with one source file. But then he went on to say this:

Each source file is separately compiled, meaning that the object file contains machine language only for the source code file that was compiled.

This suggests that the source files are still separate when it gets to the compiling stage but I thought that all the files had been pasted into one big file. Can someone explain, are all the files pasted into one file or are they left separate and later connected by the linker?

  • 1
    *" all of the files are pasted"* - all of the **included** files are pasted into a single source called a *translation unit*. Different source files with different stacks of include files will be regarded as different translation units. Each translation unit is compiled, then the resulting object code from each is linked together to form your program. – WhozCraig Jun 21 '18 at 08:21
  • so there is something called a source file which is like the main file, and you will have multiple source files in your program with each source file containing different smaller files? If so then what defines something as a source file over a smaller file that would be included in a source file? – Ameen Izhac Jun 21 '18 at 13:17
  • @AmeenIzhac what you said is partially correct. Yes there's a "big boss" source file. A entry point, where all things get started. By default its name `main.cpp`, have to be this name, or compiler doesn't know what to compile and may have *can't find main* error (you can change it with settings). I think you may need to read more about C++ and see more examples. – Rick Jun 21 '18 at 13:26

3 Answers3

1

Your last point is correct. (Following file endings are true for Linux systems)

After the preprocessor has done its job, the compiler compiles every source file (.cpp) into one separate object file (.o).

Afterwards the linker is putting them together into an executable, a shared library (.so) or a static library (.a).

Have a look at this question for more information: How does the compilation/linking process work?

izlin
  • 2,129
  • 24
  • 30
  • thanks but my question is that how does the compiler compile "every source file into one separate object file" if all the files have been pasted into a single file already back in the preprocessor – Ameen Izhac Jun 21 '18 at 12:59
  • @AmeenIzhac only the header files and preprocessor commands getting pasted into a source file and not the sources of other files. Example: source file A and B and a header file C that is used by both results in CA and CB and NOT int CAB. Now CA and CB can be compiled entirely separately. – izlin Jun 21 '18 at 13:08
  • So what your saying is that there is a difference between a source file and a header file; A source file is like one of the main files and header files are smaller files you create to include within source files? If what I just said is correct, then what defines something as a source file over a header file; is it like a certain amount of lines you must exceed consider a file a source file or what? – Ameen Izhac Jun 21 '18 at 13:23
  • @AmeenIzhac the files ending with .cpp are your source files and the ones with .h are your header files. Length doesn't matter. Have a look at this http://www.learncpp.com/cpp-tutorial/19-header-files/ (or any other tutorial that explains header files, they explain it much better than I ever could) – izlin Jun 22 '18 at 06:35
1

In C++ we separate header files (.h or .hpp) and code files (.cpp) in the header files you usually define your object structure and in the code you write the code to implement its logic.

at the top of your code file you usually include a set of headers

#include <iostream>
#incluie <string>

The pre-processor will take the definition of the classes defined in these headers and create a big file with all the definitions.

The code file itself will be compiled on its own to a single .o file

For example:

song.h

#include <string>

class Song {
   public:
      std::string getLyrics();
};

person.h

#include "song.h"  // Since person sing a Song

class Person {
   public:
      void sing(Song song);
};

So in this case if you do this in your code

main.cpp

#include "person.h"

int main() {
    Person person;
    Song song;

    person.sing(song);
}

The preprocessor will combine your main.cpp with the headers into a big file. And this is what the compiler will see.

class string { ...}

class Song {
   public:
      std::string getLyrics();
};

class Person {
   public:
      void sing(Song song);
};

int main() {
    Person person;
    Song song;

    person.sing(song);
}

Now when you add the implementation into .cpp files. each implementation will be compiled separately (song, person).

Song.cpp

#include "song.h" // get the definition of Song

 std::string Song::getLyrics() {
    return "Every little thing, gonna be alright (bob Marley)\n";
}

Person.cpp

#include "person.h" // get the definition of Person

// Implementation
void Person::sing(Song song) {
    std::cout << song.getLyrics();
}

The next step is the linker, it links between the compiled files.

i.e. link between main.o, person.o and song.o so main can create a person that can sing a Song

Nir
  • 1,608
  • 1
  • 17
  • 29
  • so all of the code is pasted into one file but the compiler knows the difference between which code is from which file and compiles each section of code from different files separately into different compiled files and then later the linker links the compiled files? – Ameen Izhac Jun 21 '18 at 11:59
  • @AmeenIzhac Not exactly, the compiler does not know which code is from which file. It doesn't really care. It knows that all required definitions from included headers is there, so the code can use it. In my example, definition of Song & Person, so the compiler knows the structure and main can use them. – Nir Jun 21 '18 at 12:24
  • Ok so if all the code is in one file then why are separate object files produced? – Ameen Izhac Jun 21 '18 at 12:58
  • @AmeenIzhac Because it saves your time when recompiling the whole thing. For exmaple, you have `a.cpp` and `b.cpp`, you get `a.o` and `b.o` and a exe let's say `a.exe`. When you edit your code in `b.cpp` and recompile, you dont have to compile `a.cpp` again, you can use `a.o` again, only compile `b.cpp` again, get the new `b.o` and new `a.exe`. It saves time when you deal with a large project. – Rick Jun 21 '18 at 13:33
  • ok thanks_________ – Ameen Izhac Jun 21 '18 at 16:50
1

I used to see a well-explained ascii graph illustration from an answer but can't find it now.

I found some similar images.

enter image description here


enter image description here

Basically there are 3 stages you need to know, Preprocessing stage, Compiling Stage and Linking stage. I read the answer provided above before, the detail is not much useful for beginner.

Rick
  • 7,007
  • 2
  • 49
  • 79
  • Ok thanks for the diagram but as I explained in the question, you have source file 1,2 and 3 all being separately assembled but what I understood is that all of those source files are put into one source file and then that source file is then assembled as one – Ameen Izhac Jun 21 '18 at 13:02
  • @AmeenIzhac No. if you have 3 `.cpp` then you have 3 corresponding `.o`, and after linking stage, 3 `.o` files put into 1 executable file. Not what you said. No one include another `.cpp` in a `.cpp`. Only include `.h`, and most thing in `.h` are for declaration, which means let code in that `.cpp` know there are some thing for example functions you can call. But the implementions are in other source files. – Rick Jun 21 '18 at 13:20
  • Oh ok thanks, what I didnt understand was that there is a difference between a .cpp file and a .h file. Also what is the difference between declaration and implementation? – Ameen Izhac Jun 21 '18 at 13:27
  • @AmeenIzhac They are just files with different suffix, from the point of view of a editor. But the compiler treats them differently. If you don't even know the concept of declaration and implementation, I think you need to read some basic knowledge about C++ instead of wasting your time here, waiting for someone to explain. You can Google such stuff easily. – Rick Jun 21 '18 at 13:39
  • I know what declaration is like declaring variables and functions and i know what implementation is like it basically means to code something but really all code is implementation so even the header files r implementation so i was wondering if u could elaborate abit more on what is on each type of file – Ameen Izhac Jun 21 '18 at 16:48
  • @AmeenIzhac Mostly, we put declarations in header files. Coz you can repeat declaration but not implementation. If you put a complete definition in a header file, then other header file may include it. E.g. a.h has a definition of function A, b.h include A, as a result, B also have a copy of function A. When main.cpp include a.h and b.h, 2 same implementation of function A, then you get error. But if you only put declarations, that's fine. You can have duplicated declarations. And you need to compile related a.cpp and b.cpp to make sure that those implementation can be found in linking stage. – Rick Jun 22 '18 at 02:51
  • Btw, there are functions or variable can define in header files without collision, but that's another story. – Rick Jun 22 '18 at 02:51