I have been reading the 2nd edition of A Tour of C++ by Bjarne Stroustroup and in section 3.2 on Separate Compilation, he mentions translation units.
A
.cppfile that is compiled by itself (including thehfiles it#includes) is called a translation unit. A program can consist of many thousand translation units.
I tried to follow along the prior chapters and build up the code for Vector and wrote out the .h and .cpp files for this diagram also in the same section.
vector.h
class Vector { public: Vector(int s) : elem{new double[s]}, sz{s} {} int size(); double &operator[](int i); private: int sz; // the number of elements double *elem; // pointer to the elements }; vector.cpp
#include "vector.h" double &Vector::operator[](int i) { return elem[i]; } int Vector::size() { return sz; } int main() { } user.cpp - own implementation to test things out
#include "vector.h" #include <iostream> int main() { Vector v{4}; std::cout << v.size() << std::endl; } When I built the object file for vector with g++, I got a linker error and I was able to resolve this with writing a main function in Vector.cpp.
$ g++ vector.cpp /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/Scrt1.o: in function `_start': (.text+0x1b): undefined reference to `main' collect2: error: ld returned 1 exit status Based on the diagram above, that appears in the same section of the book, it seems to me that user.cpp would also need its own main function as I can't compile it without main as I get the same error as above but I can't compile successfully without adding vector.cpp in the command to g++.
However, when I compile both, I get an error that I have multiple main functions.
$ g++ vector.cpp user.cpp /usr/bin/ld: /tmp/ccX6Gtco.o: in function `main': user.cpp:(.text+0x0): multiple definition of `main'; /tmp/ccdQHbRR.o:vector.cpp:(.text+0x3a): first defined here collect2: error: ld returned 1 exit status - How can I build
userwithout this error? Becauseg++ user.cppcomplains thatsizeisn't defined. - Does it mean that a translation unit must have a
mainfunction because that's the only way to compile it? - If the only way to build
user.cppis withvector.cpp, is this compilation different from the definition of a translation unit? Isn't this a different relationship compared to what is shown in the diagram where both arrows point tovector.h?
This question says that:
A single translation unit can be compiled into an object file, library, or executable program.
- How can the compilation work without
mainin either case?

mainin a program comprised of one-or-more translation units (object files).mainin the whole program. Your first error was because you were trying to compilevector.cppand link it into an executable. It's the last step that needsmain, and that failed.g++ main.cppattempts to do all these steps at the same time. If you want only to preprocess and compile a single translation unit (without linking), pass-cswitch to gcc execution. I expect the book to go in detail of the process a bit later?