usually, cmake needs to know source folder and build folder
and below are the most common cmake commands that you would use professionally.
- To config and generate your build folder from your CMakeLists.txt (CMakeLists.txt lives under your project source folder)
$cmake -S . -B build
or just
$cmake -B build
-S is where your project source is located so if you are already in your project source folder you simply typed -S . or just omitted it entirely like the second version so . (dot) mean current dir
-B is where your build output is located. if build folder does not exist cmake will automatically create one.
- by default, it sets build mode to Release mode so if you want to build debug so you can debug it you can simple tell cmake during build config as seen below
$cmake -B build -DCMAKE_BUILD_TYPE=Debug
- Last but not least, when you are ready to compile your project you simple tell cmake below
$cmake --buid build
where build is the build folder generated from earlier step
Note:
- you can add — -j8 to the compilation whcih mean you want it to use 8 compilation jobs in parallel. 8 also means usually 8 processors.
cmake --build build -- -j8
- if you want to compile specific targets (your binary, linked library etc) you can simple tell cmake
$cmake --build build --target myLib -- -j8
$cmake --build build --target net -- -j8
where myLib, net are what you target as linked libraries in your CMakeLists.txt
e.g:
add_library(mylib STATIC lib.cpp)
add_library(net SHARED net.cpp)
$cmake --build build --target myApp -- -j8
where myApp is what you target as your binary in your CMakeLists.txt
e.g:
add_executable(myapp main.cpp)
- To do cmake clean but keep build folder
cmake --build build --target clean
- To do a full cmake clean: you simply remove the build folder and regenerate it
$ rm -rf build
$ cmake -B build
