Skip to content

Commit f61a101

Browse files
committed
add third-party library
1 parent f9fd17e commit f61a101

File tree

318 files changed

+130263
-5
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

318 files changed

+130263
-5
lines changed

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,12 @@ add_library(math src/math.cc)
99
# Add definition for the cmake-tutorial binary
1010
add_executable(cmake-tutorial src/main.cc)
1111
target_link_libraries(cmake-tutorial math)
12+
13+
# Third-party library
14+
add_subdirectory(lib/googletest)
15+
16+
# Test
17+
add_executable(math_test test/math_test.cc)
18+
target_link_libraries(math_test gtest)
19+
target_link_libraries(math_test math)
20+

README.md

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,39 @@
11
# CMake Tutorial
2-
This tutorial contains step-by-step how to setup CMake on your
3-
C++ project.
2+
This tutorial cover the following:
3+
4+
1. Build the project using simple `c++(1)` and `make(1)`.
5+
2. Build the project using `cmake(1)`.
6+
3. Build the project using `cmake(1)` with third party library.
7+
8+
9+
In this tutorial we will use the following project structure:
10+
11+
```
12+
cmake-tutorial/
13+
├── CMakeLists.txt
14+
├── README.md
15+
├── lib
16+
│   └── googletest
17+
├── src
18+
│   ├── main.cc
19+
│   ├── math.cc
20+
│   └── math.h
21+
└── test
22+
└── math_test.cc
23+
```
24+
25+
Directory structure:
26+
27+
- `lib` : Directory for third party library.
28+
- `src` : Directory for source code.
29+
- `test` : Directory for test.
30+
31+
`src/main.cc` is our main executable and `src/math.{cc,h}` is an internal
32+
library that used by `src/main.cc`.
33+
34+
We will start from the basic on how to build the project using `c++(1)` only
35+
and a simple `Makefile`. Then we define the build in `CMakeLists.txt` and
36+
using `cmake(1)` to generate complex `Makefile` for us.
437

538

639
## Install CMake
@@ -49,13 +82,15 @@ Now we can run:
4982

5083
make build
5184

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:
85+
to build `cmake-tutorial` binary. If there are no changes in
86+
`src/{main,math}.cc` and `src/math.h`, the subsequent command
87+
will do nothing:
5488

5589
% make build
5690
make: Nothing to be done for `build'.
5791

58-
this is usefull when working on larger project, we only compile the object that changes.
92+
this is usefull when working on larger project, we only compile the
93+
object that changes.
5994

6095

6196
## Using CMake
@@ -94,6 +129,42 @@ Now we can run `make cmake-tutorial` to build the binary.
94129
[100%] Built target cmake-tutorial
95130

96131

132+
## Using CMake with 3rd-party library
133+
Suppose that we want to write a unit test for `math::add(a, b)`. We will use a
134+
[googletest](https://github.com/google/googletest) library to create and run the
135+
unit test.
136+
137+
Add the following definition to `CMakeLists.txt`:
138+
139+
# Third-party library
140+
add_subdirectory(lib/googletest)
141+
142+
# Test
143+
add_executable(math_test test/math_test.cc)
144+
target_link_libraries(math_test gtest)
145+
target_link_libraries(math_test math)
146+
147+
Re-generate the build files using the following command:
148+
149+
cmake .
150+
151+
Build the unit test:
152+
153+
make math_test
154+
155+
Run the test:
156+
157+
% ./math_test
158+
[==========] Running 1 test from 1 test case.
159+
[----------] Global test environment set-up.
160+
[----------] 1 test from MathAddTest
161+
[ RUN ] MathAddTest.PositiveNum
162+
[ OK ] MathAddTest.PositiveNum (0 ms)
163+
[----------] 1 test from MathAddTest (0 ms total)
97164

165+
[----------] Global test environment tear-down
166+
[==========] 1 test from 1 test case ran. (0 ms total)
167+
[ PASSED ] 1 test.
98168

169+
Done.
99170

lib/.DS_Store

8 KB
Binary file not shown.

lib/googletest/.gitignore

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Ignore CI build directory
2+
build/
3+
xcuserdata
4+
cmake-build-debug/
5+
.idea/
6+
bazel-bin
7+
bazel-genfiles
8+
bazel-googletest
9+
bazel-out
10+
bazel-testlogs
11+
# python
12+
*.pyc
13+
14+
# Visual Studio files
15+
*.sdf
16+
*.opensdf
17+
*.VC.opendb
18+
*.suo
19+
*.user
20+
_ReSharper.Caches/
21+
Win32-Debug/
22+
Win32-Release/
23+
x64-Debug/
24+
x64-Release/
25+
26+
# Ignore autoconf / automake files
27+
Makefile.in
28+
aclocal.m4
29+
configure
30+
build-aux/
31+
autom4te.cache/
32+
googletest/m4/libtool.m4
33+
googletest/m4/ltoptions.m4
34+
googletest/m4/ltsugar.m4
35+
googletest/m4/ltversion.m4
36+
googletest/m4/lt~obsolete.m4
37+
38+
# Ignore generated directories.
39+
googlemock/fused-src/
40+
googletest/fused-src/

lib/googletest/.travis.yml

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Build matrix / environment variable are explained on:
2+
# http://about.travis-ci.org/docs/user/build-configuration/
3+
# This file can be validated on:
4+
# http://lint.travis-ci.org/
5+
6+
sudo: false
7+
language: cpp
8+
9+
# Define the matrix explicitly, manually expanding the combinations of (os, compiler, env).
10+
# It is more tedious, but grants us far more flexibility.
11+
matrix:
12+
include:
13+
- os: linux
14+
compiler: gcc
15+
sudo : true
16+
install: ./ci/install-linux.sh && ./ci/log-config.sh
17+
script: ./ci/build-linux-bazel.sh
18+
- os: linux
19+
compiler: clang
20+
sudo : true
21+
install: ./ci/install-linux.sh && ./ci/log-config.sh
22+
script: ./ci/build-linux-bazel.sh
23+
- os: linux
24+
group: deprecated-2017Q4
25+
compiler: gcc
26+
install: ./ci/install-linux.sh && ./ci/log-config.sh
27+
script: ./ci/build-linux-autotools.sh
28+
- os: linux
29+
group: deprecated-2017Q4
30+
compiler: gcc
31+
env: BUILD_TYPE=Debug VERBOSE=1 CXX_FLAGS=-std=c++11
32+
- os: linux
33+
group: deprecated-2017Q4
34+
compiler: clang
35+
env: BUILD_TYPE=Debug VERBOSE=1
36+
- os: linux
37+
group: deprecated-2017Q4
38+
compiler: clang
39+
env: BUILD_TYPE=Release VERBOSE=1 CXX_FLAGS=-std=c++11
40+
- os: osx
41+
compiler: gcc
42+
env: BUILD_TYPE=Debug VERBOSE=1
43+
if: type != pull_request
44+
- os: osx
45+
compiler: gcc
46+
env: BUILD_TYPE=Release VERBOSE=1 CXX_FLAGS=-std=c++11
47+
if: type != pull_request
48+
- os: osx
49+
compiler: clang
50+
env: BUILD_TYPE=Debug VERBOSE=1
51+
if: type != pull_request
52+
- os: osx
53+
compiler: clang
54+
env: BUILD_TYPE=Release VERBOSE=1 CXX_FLAGS=-std=c++11
55+
if: type != pull_request
56+
57+
# These are the install and build (script) phases for the most common entries in the matrix. They could be included
58+
# in each entry in the matrix, but that is just repetitive.
59+
install:
60+
- ./ci/install-${TRAVIS_OS_NAME}.sh
61+
- . ./ci/env-${TRAVIS_OS_NAME}.sh
62+
- ./ci/log-config.sh
63+
64+
script: ./ci/travis.sh
65+
66+
# For sudo=false builds this section installs the necessary dependencies.
67+
addons:
68+
apt:
69+
# List of whitelisted in travis packages for ubuntu-precise can be found here:
70+
# https://github.com/travis-ci/apt-package-whitelist/blob/master/ubuntu-precise
71+
# List of whitelisted in travis apt-sources:
72+
# https://github.com/travis-ci/apt-source-whitelist/blob/master/ubuntu.json
73+
sources:
74+
- ubuntu-toolchain-r-test
75+
- llvm-toolchain-precise-3.7
76+
packages:
77+
- g++-4.9
78+
- clang-3.7
79+
80+
notifications:
81+
email: false

0 commit comments

Comments
 (0)