Switch branches/tags
Nothing to show
Find file History
Latest commit 09d686f May 1, 2012 @matiu2 Lesson one done
Permalink
..
Failed to load latest commit information.
CMakeLists.txt
README
build.sh
main.cpp

README

=================================
Overview:
=================================

This lesson shows how to use CMAKE to compile and run a simple c++ application that prints out hello world.

Getting ready to compile on ububntu:

    sudo apt-get update
    sudo apt-get install build-essential cmake cmake-curses-gui gcc

=================================
File Descriptions:
=================================

Each file is full of comments so go in there and have a good read

 1. main.cpp - This is the C++ source file that prints 'hello world' on the screen
 2. CMakeLists.txt - This file describes how to build the application and is run by the cmake program
 3. build.sh - shows the steps used to build the project

=================================
Description of the build process:
=================================

Here we expand on the contents of the build.sh script a bit:

> mkdir build
> cd build

We make a separate directory for building so we don't clutter up the source tree. You could also put this completely out of the source tree in a separate ~/builds/hello der for example.

> cmake ..

This runs the cmake command, telling it to search through the source tree for CMakeList.txt files.

CMake will find the CMakeList.txt files and generate gnu make files for us. It can also generate other kinds of make files and IDE project files: http://www.cmake.org/Wiki/CMake_Generator_Specific_Information

Those makefiles that are generated in the 'build' directory. You can see the contents in the build/Makefile file.

> make

This runs the make command, which will read the MakeFile and run the compiler and linker. Lets look a bit deeper at what's going on. try this:

> make clean && make VERBOSE=true

There are two interesting lines in this output. One is the command to compile the source file:

> /home/matiu/gcc/bin/c++     -o CMakeFiles/hello.dir/main.cpp.o -c /shared/documents/projects/wt-tutorial/lesson-00-hello-world-in-cpp/main.cpp

That compiles the main.cpp file in the source directory into the 'CMakeFiles/hello.dir/main.cpp.o' object file. The object file is like a little library of data; it contains several chunks of data, each one could be a string to print, a function in machine language, or some other fun thing. We'll take a look inside shortly.

The other line is the linker command:

> /home/matiu/gcc/bin/c++       CMakeFiles/hello.dir/main.cpp.o  -o hello -rdynamic 

that takes the object files (only one in this case) and links them all together in an ELF executable file.

==============================
Changing the compiler settings
==============================

Here we'll change the settings in the cmake cache file, telling it to generate Makefiles that call g++ with the '-g' command line parameter. That parameter will make g++ include a lot more info in the object file, and the executable.

So in the build directory run:

> ccmake ..

This will show a menu allowing you to change different parameters for the build.

Hit enter to edit the 'CMAKE_BUILD_TYPE', type the value 'Debug' and hit enter to save it.

Hit 'c' to reconfigure, then 'g' to generate the new Makefile and exit the gui.

Now run:

> make VERBOSE=true

Notice that the '-g' is passed to the compiler this time.

==============================
Looking inside the object file
==============================

From the build directory, move to the directory where the object file (that was generated by the first C++ command) is:

> cd CMakeFiles/hello.dir/

Now lets have a look at the 'objects' that are available in that file:

> objdump main.cpp.o -t

Each line of the output is the name of something that the linker can use later to build the executable. For example:

0000000000000000 l    d  .rodata        0000000000000000 .rodata

holds the string "Hello World"

0000000000000000 g     F .text  0000000000000032 main

holds the machine code for the main function.

Let's have a look at the data:

> objdump main.cpp.o -j .rodata -s

Nice eh. That's your hello world text.

Lets have a look at the assembly language:

> objdump main.cpp.o -j .text -Sl

That shows the line numbers in the source file, and the source code lines, then the assembly language the compiler turned it into.

That's it for lesson one :)