Linking files in g++

You probably tried to either compile and link instead of just compiling source files or somehow forgot something.

Variation one (everything in one line; recompiles everything all the time):

g++ -o myexecutable first.cpp second.cpp third.cpp [other dependencies, e.g. -Lboost, -LGL, -LSDL, etc.]

Variation two (step by step; if no -o is provided, gcc will reuse the input file name and just change the extension when not linking; this variation is best used for makefiles; allows you to skip unchanged parts):

g++ -c first.cpp
g++ -c second.cpp
g++ -c third.cpp
g++ -o myexecutable first.o second.o third.o [other dependencies]

Variation three (some placeholders):

Won’t list it but the parameters mentioned above might as well take placeholders, e.g. g++ -c *.cpp will compile all cpp files in current directory to o(bject) files of the same name.

Overall you shouldn’t worry too much about it unless you really have to work without any IDE. If you’re not that proficient with the command line syntax, stick to IDEs first.

Leave a Comment