I have been learning C++ in school to create small command-line programs.
However, I have only built my projects with IDEs, including VS08 and QtCreator.
I understand the process behind building a project: compile source to object code, then link them into an executable that is platform specific (.exe, .app, etc). I also know most projects also use make to streamline the process of compiling and linking multiple source and header files.
The thing is, although IDEs do all this under the hood, making life very easy, I don’t really know what is really happening, and feel that I need to get accustomed to building projects the “old fashioned way”: from the command line, using the tool chain explicitly.
I know what Makefiles are, but not how to write them.
I know what gcc does, but not how to use it.
I know what the linker does, but not how to use it.
What I am looking for, is either an explanation, or link to a tutorial that explains, the workflow for a C++ project, from first writing the code up to running the produced executable.
I would really like to know the what, how, and why of building C++.
(If it makes any difference, I am running Mac OS X, with gcc 4.0.1 and make 3.81)
Thanks!
Compiling
Let’s say you want to write a simple ‘hello world’ application. You have 3 files,
hello.cpphello-writer.cppandhello-writer.h, the contents beingThe *.cpp files are converted to object files by
g++, using the commandsThe
-cflag skips the linking for the moment. To link all the modules together requires runningcreating the program
hello. If you need to link in any external libraries you add them to this line, eg-lmfor the math library. The actual library files would look something likelibm.aorlibm.so, you ignore the suffix and the ‘lib’ part of the filename when adding the linker flag.Makefile
To automate the build process you use a makefile, which consists of a series of rules, listing a thing to create and the files needed to create it. For instance,
hello.odepends onhello.cppandhello-writer.h, its rule isIf you want to read the make manual, it tells you how to use variables and automatic rules to simplify things. You should be able to just write
and the rule will be created automagically. The full makefile for the hello example is
Remember that indented lines must start with tabs. Not that not all rules need an actual file, the
alltarget just says createhello. It is common for this to be the first rule in the makefile, the first being automatically created when you runmake.With all this set up you should then be able to go to a command line and run
More advanced Makefile stuff
There are also some useful variables that you can define in your makefile, which include
Additional flags to pass to the
compiler (E.g include directories
with -I)
pass to the linker
to link
link)
Define variables using
=, add to variables using+=.The default rule to convert a .cpp file to a .o file is
where
$<is the first dependancy and$@is the output file. Variables are expanded by enclosing them in$(), this rule will be run with the patternhello.o:hello.cppSimilarly the default linker rule is
where
$^is all of the prerequisites. This rule will be run with the patternhello:hello.o hello-writer.o. Note that this uses the c compiler, if you don’t want to override this rule and are using c++ add the library-lstdc++toLDLIBSwith the linein the makefile.
Finally, if you don’t list the dependancies of a
.ofile make can find them itself, so a minimal makefile might beNote that this ignores the dependancy of the two files on
hello-writer.h, so if the header is modified the program won’t be rebuilt. If you’re interested, check the-MDflag in the gcc docs for how you can automatically generate this dependancy.Final makefile
A reasonable final makefile would be