@@ -31,6 +31,7 @@ We will start from the basic on how to build the project using `c++(1)` only
3131and a simple ` Makefile ` . Then we define the build in ` CMakeLists.txt ` and
3232using ` cmake(1) ` to generate complex ` Makefile ` for us.
3333
34+
3435## Install CMake
3536
3637First of all, you need to install ` cmake ` .
@@ -62,6 +63,7 @@ Or we can do the compile and linking on the separate steps
6263 c++ -c src/math.cc -o math.o
6364 c++ src/main.cc math.o -o cmake-tutorial
6465
66+
6567## Using Makefile
6668
6769We can automate the step to compile and link above using ` Makefile ` .
@@ -87,6 +89,7 @@ the subsequent command will do nothing:
8789
8890this is useful when working on larger project, we only compile the object that changes.
8991
92+
9093## Using CMake
9194
9295Now we know how to perform compiling and linking using the ` C++ ` and ` make ` command.
@@ -110,6 +113,12 @@ We can generate the `Makefile` based on the definition above using the following
110113
111114 cmake .
112115
116+ Or create a ` build ` directory to store the generated files by CMake:
117+
118+ mkdir build
119+ cd build/
120+ cmake ..
121+
113122Now we can run ` make cmake-tutorial ` to build the binary.
114123
115124 % make cmake-tutorial
@@ -122,6 +131,11 @@ Now we can run `make cmake-tutorial` to build the binary.
122131 [100%] Linking CXX executable cmake-tutorial
123132 [100%] Built target cmake-tutorial
124133
134+ Or we can use the CMake directly via:
135+
136+ cmake --build . --target cmake-tutorial
137+
138+
125139## Using CMake with 3rd-party library
126140
127141Suppose that we want to write a unit test for ` math::add(a, b) ` .
@@ -159,11 +173,12 @@ Add the following definition to `CMakeLists.txt`:
159173
160174Re-generate the build files using the following command:
161175
162- cmake .
176+ cd build/
177+ cmake ..
163178
164179Build the unit test:
165180
166- make math_test
181+ cmake --build . --target math_test
167182
168183Run the test:
169184
0 commit comments