|
| 1 | +# CMake Tutorial |
| 2 | +This tutorial contains step-by-step how to setup CMake on your |
| 3 | +C++ project. |
| 4 | + |
| 5 | + |
| 6 | +## Install CMake |
| 7 | +First of all, you need to install `cmake`. |
| 8 | + |
| 9 | +On Ubuntu: |
| 10 | + |
| 11 | + sudo apt-get install cmake |
| 12 | + |
| 13 | +On macOS: |
| 14 | + |
| 15 | + brew install cmake |
| 16 | + |
| 17 | +Make sure the `cmake` is installed correctly: |
| 18 | + |
| 19 | + % cmake --version |
| 20 | + cmake version 3.10.2 |
| 21 | + |
| 22 | + CMake suite maintained and supported by Kitware (kitware.com/cmake). |
| 23 | + |
| 24 | + |
| 25 | +## Compiling & Linking |
| 26 | +We can build this project using the following command: |
| 27 | + |
| 28 | + c++ src/main.cc src/math.cc -o cmake-tutorial |
| 29 | + |
| 30 | +Or we can do the compile and linking on the separate steps |
| 31 | + |
| 32 | + c++ -c src/math.cc -o math.o |
| 33 | + c++ src/main.cc math.o -o cmake-tutorial |
| 34 | + |
| 35 | + |
| 36 | +## Using Makefile |
| 37 | +We can automate the step to compile and link above using `Makefile`. |
| 38 | +First we need to write a `Makefile` with the following content: |
| 39 | + |
| 40 | + math.o: src/math.cc src/math.h |
| 41 | + c++ -c src/math.cc -o math.o |
| 42 | + |
| 43 | + cmake-tutorial: math.o |
| 44 | + c++ src/main.cc math.o -o cmake-tutorial |
| 45 | + |
| 46 | + build: cmake-tutorial |
| 47 | + |
| 48 | +Now we can run: |
| 49 | + |
| 50 | + make build |
| 51 | + |
| 52 | +to build `cmake-tutorial` binary. If there are no changes in `src/{main,math}.c` and |
| 53 | +`src/math.h`, the subsequent command will do nothing: |
| 54 | + |
| 55 | + % make build |
| 56 | + make: Nothing to be done for `build'. |
| 57 | + |
| 58 | +this is usefull when working on larger project, we only compile the object that changes. |
| 59 | + |
| 60 | + |
| 61 | +## Using CMake |
| 62 | +Now we know how to perform compiling and linking using the `C++` and |
| 63 | +`make` command. Now we can use `cmake` to do all of this for us. |
| 64 | + |
| 65 | +Create new `CMakeLists.txt` with the following content: |
| 66 | + |
| 67 | + cmake_minimum_required (VERSION 3.10) |
| 68 | + |
| 69 | + # Define the project |
| 70 | + project(cmake-tutorial) |
| 71 | + |
| 72 | + # Add definition for math library |
| 73 | + add_library(math src/math.cc) |
| 74 | + |
| 75 | + # Add definition for the cmake-tutorial binary |
| 76 | + add_executable(cmake-tutorial src/main.cc) |
| 77 | + target_link_libraries(cmake-tutorial math) |
| 78 | + |
| 79 | +We can generate the `Makefile` based on the definition above using the following |
| 80 | +command: |
| 81 | + |
| 82 | + cmake . |
| 83 | + |
| 84 | +Now we can run `make cmake-tutorial` to build the binary. |
| 85 | + |
| 86 | + % make cmake-tutorial |
| 87 | + Scanning dependencies of target math |
| 88 | + [ 25%] Building CXX object CMakeFiles/math.dir/src/math.cc.o |
| 89 | + [ 50%] Linking CXX static library libmath.a |
| 90 | + [ 50%] Built target math |
| 91 | + Scanning dependencies of target cmake-tutorial |
| 92 | + [ 75%] Building CXX object CMakeFiles/cmake-tutorial.dir/src/main.cc.o |
| 93 | + [100%] Linking CXX executable cmake-tutorial |
| 94 | + [100%] Built target cmake-tutorial |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
0 commit comments