Skip to content

Commit f9fd17e

Browse files
committed
Init
0 parents  commit f9fd17e

File tree

6 files changed

+145
-0
lines changed

6 files changed

+145
-0
lines changed

.gitignore

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

CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cmake_minimum_required (VERSION 3.10)
2+
3+
# Define the project
4+
project(cmake-tutorial)
5+
6+
# Add definition for math library
7+
add_library(math src/math.cc)
8+
9+
# Add definition for the cmake-tutorial binary
10+
add_executable(cmake-tutorial src/main.cc)
11+
target_link_libraries(cmake-tutorial math)

README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+

src/main.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <iostream>
2+
#include "math.h"
3+
4+
using namespace std;
5+
6+
int main(int argc, char **argv) {
7+
int result = math::add(1, 2);
8+
cout << "result : " << result << endl;
9+
}

src/math.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace math {
2+
3+
int add(int a, int b) {
4+
return a + b;
5+
}
6+
7+
}
8+

src/math.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef CMAKE_TUTORIAL_MATH_H
2+
#define CMAKE_TUTORIAL_MATH_H
3+
4+
namespace math {
5+
6+
int add(int a, int b);
7+
8+
}
9+
10+
#endif

0 commit comments

Comments
 (0)