My note from CMake Tutorial
Episode 1 Summarized by Komsun Tamanakijprasart
- Create and name a new file makefile in your working directory
- makefile doesn't accept space, use tab instead
Easy example:
makefile
default: g++ main.cpp -o out
You just need to type make
in the terminal to execute the makefile (compile and build in this case)
make
to execute the makefile./out
to run the application
Linux or Windows terminal
make ./out
- CMake relies on a top-level file called CMakeLists.txt (exact spelling!)
Project Workflow
Step 1: Prepare your source file main.cpp
Step 2: Create CMakeLists.txt, a separate folder for built files, and run cmake
Step 3: Run make
2 Important points in CMakeList
- Where your source file is (contain CMakeLists.txt)
- Where your build folder is
Linux terminal
cmake -S <path-to-source> -B <path-to-build>
Ex:
Linux terminal
cmake -S . -B build/
-S .
means the source (and CMakeList.txt) is located in the current directory
CMake_Learn/try_CMakeList/main.cpp
Lines 1 to 7 in eee93c1
CMakeLists.txt
cmake_minimum_required(VERSION 3.16.3) project(myProject) add_executable(${PROJECT_NAME} main.cpp)
What are these lines?
cmake_minimum(VERSION xxxxx)
: Minimum required versionproject(projectName)
: Add a projectadd_executable(programName source)
: Build the executable file${PROJECT_NAME}
: Built-in keyword to refer to the project name
Linux Terminal
cmake -S . -B obj/
Result
Linux Terminal - at where the makefile is located
make
Results
Notes:
Linux terminal
cmake --version
CMakeLists.txt
cmake_minimum_required(VERSION 3.16.3) project(Trim) add_executable(trim main.cpp) install(TARGETS targetName DESTINATION bin)
What are these lines?
install(TARGETS targetName DESTINATION xxx)
: specify where to install the executable file (or library), bin is a standard place to install (usually usr/local/bin/)- Usually, we create a separate folder for the built files (in this case, build)
Linux terminal
cmake -S ..
-S ..
means the source (and CMakeLists.txt) is in the previous directory- no
-B
means the current directory is used for build
Not sure what to make
? type make help
install(TARGETS <targetName> DESTINATION <destination>)
Example:
CMakeLists.txt
cmake_minimum_required(VERSION 3.16.3) project(Trim) add_executable(trim main.cpp) install(TARGETS trim DESTINATION bin)
Install the program, as specified in CMakeLists.txt
Linux terminal
sudo make install
Because we install
the program on the bin already, we can call this program ANYWHERE !! (Don't need to locate the program's directory anymore!)