Skip to content

Commit 67814b6

Browse files
authored
Update README (#7)
1 parent 953e3c0 commit 67814b6

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

.gitignore

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
# Generated by CMake
2-
CMakeCache.txt
3-
CMakeFiles/
4-
Makefile
5-
cmake-tutorial
6-
cmake_install.cmake
7-
*.a
8-
*.o
1+
# Build directory
2+
build/
93

4+
# macOS
105
.DS_Store
116

7+
# IDE
128
/.idea/
13-
/cmake-build-*/
9+
/cmake-build-*/

CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ cmake_minimum_required (VERSION 3.10)
33
# Define the project
44
project(cmake-tutorial)
55

6+
67
# Add definition for math library
78
add_library(math src/math.cc)
89

10+
911
# Add definition for the cmake-tutorial binary
1012
add_executable(cmake-tutorial src/main.cc)
1113
target_link_libraries(cmake-tutorial math)
1214

15+
1316
# Third-party library
1417
include(ExternalProject)
1518
ExternalProject_Add(googletest
@@ -18,22 +21,30 @@ ExternalProject_Add(googletest
1821
GIT_TAG "master"
1922
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/lib/installed
2023
)
24+
25+
2126
# Prevent build on all targets build
2227
set_target_properties(googletest PROPERTIES EXCLUDE_FROM_ALL TRUE)
2328

29+
2430
# Define ${CMAKE_INSTALL_...} variables
2531
include(GNUInstallDirs)
2632

33+
2734
# Specify where third-party libraries are located
2835
link_directories(${CMAKE_BINARY_DIR}/lib/installed/${CMAKE_INSTALL_LIBDIR})
2936
include_directories(${CMAKE_BINARY_DIR}/lib/installed/${CMAKE_INSTALL_INCLUDEDIR})
3037

38+
3139
# This is required for googletest
3240
find_package(Threads REQUIRED)
3341

42+
3443
# Test
3544
add_executable(math_test test/math_test.cc)
3645
target_link_libraries(math_test math gtest Threads::Threads)
46+
47+
3748
# Make sure third-party is built before executable
3849
add_dependencies(math_test googletest)
3950
set_target_properties(math_test PROPERTIES EXCLUDE_FROM_ALL TRUE)

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ We will start from the basic on how to build the project using `c++(1)` only
3131
and a simple `Makefile`. Then we define the build in `CMakeLists.txt` and
3232
using `cmake(1)` to generate complex `Makefile` for us.
3333

34+
3435
## Install CMake
3536

3637
First 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

6769
We can automate the step to compile and link above using `Makefile`.
@@ -87,6 +89,7 @@ the subsequent command will do nothing:
8789

8890
this is useful when working on larger project, we only compile the object that changes.
8991

92+
9093
## Using CMake
9194

9295
Now 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+
113122
Now 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

127141
Suppose 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

160174
Re-generate the build files using the following command:
161175

162-
cmake .
176+
cd build/
177+
cmake ..
163178

164179
Build the unit test:
165180

166-
make math_test
181+
cmake --build . --target math_test
167182

168183
Run the test:
169184

0 commit comments

Comments
 (0)