CMake
CMake is a versatile tool that helps you build C/C++ projects on just about any platform you can think of.
Running CMake does not build your software. You typically need to run Makefile
Main Options
cmake [options] -S <path-to-directory-where-root-CMakeLists.txt-file-is> -B <path-to-build>
Example C++
Working Example
gt.sandbox.checkout.commit.cleanly b22d565 \
&& cd "$GT_SANDBOX_REPO/cpp/hello-world" \
&& cmake -S . -B ./build \
&& cd build \
&& make \
&& ./HelloWorldProjectName.executable
HelloWorld Example CMakeLists.txt
From HelloWorldExample CMakeLists.txt
Go to text →
# Min versin cmake check
cmake_minimum_required(VERSION 3.0)
# Project name
project(HelloWorldProjectName)
# Add executable target ${PROJECT_NAME}.executable will be the
# name of the executable.
add_executable(${PROJECT_NAME}.executable hello_world_main.cpp)
Command to reproduce:
gt.sandbox.checkout.commit 65d00c5 \
&& cd "${GT_SANDBOX_REPO}/cpp/hello-world" \
&& cmd.run.announce "cmake -S . -B ./build && cd build && make && ./HelloWorldProjectName.executable"
Recorded output of command:
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/vintrin/git_repos/glassthought-sandbox/cpp/hello-world/build
[100%] Built target HelloWorldProjectName.executable
Hello World!
Relationships
Files Generates by CMake
Files Generates by CMake Video Ref Timestamp
Don't modify files generated by CMake within your ./build path directory. Instead modify: CMakeLists.txt.
Example Files Generated By CMake
mac> cd "$GT_SANDBOX_REPO/cpp/hello-world/build" && tree
.
├── CMakeCache.txt
├── CMakeFiles
│ ├── 3.25.2
│ │ ├── CMakeCCompiler.cmake
│ │ ├── CMakeCXXCompiler.cmake
│ │ ├── CMakeDetermineCompilerABI_C.bin
│ │ ├── CMakeDetermineCompilerABI_CXX.bin
│ │ ├── CMakeSystem.cmake
│ │ ├── CompilerIdC
│ │ │ ├── CMakeCCompilerId.c
│ │ │ ├── CMakeCCompilerId.o
│ │ │ └── tmp
│ │ └── CompilerIdCXX
│ │ ├── CMakeCXXCompilerId.cpp
│ │ ├── CMakeCXXCompilerId.o
│ │ └── tmp
│ ├── CMakeDirectoryInformation.cmake
│ ├── CMakeError.log
│ ├── CMakeOutput.log
│ ├── CMakeScratch
│ ├── HelloWorldProjectName.executable.dir
│ │ ├── DependInfo.cmake
│ │ ├── build.make
│ │ ├── cmake_clean.cmake
│ │ ├── compiler_depend.make
│ │ ├── compiler_depend.ts
│ │ ├── depend.make
│ │ ├── flags.make
│ │ ├── link.txt
│ │ └── progress.make
│ ├── Makefile.cmake
│ ├── Makefile2
│ ├── TargetDirectories.txt
│ ├── cmake.check_cache
│ ├── pkgRedirects
│ └── progress.marks
├── Makefile
└── cmake_install.cmake
10 directories, 29 files
Reference
More in depth
Using CMake to install into user level bin directory.
From Installing into User Level Bin Directory cmake install
Go to text →
About
cmake install
Allows installation of your executable into widely accessible bin folders such as /usr/local/bin/
Live example using "cmake install"
gt.sandbox.checkout.commit.cleanly 56ae6c2 \
&& cd cpp/trim \
&& (rm -rf ./build; rm /usr/local/bin/trim-example-app; echo "cleaned") \
&& cmake -S . -B ./build \
&& cd build \
&& make install \
&& trim-example-app first second
Sample CMake file with install
Sample CMake with installation
cmake_minimum_required(VERSION 3.0.0)
project(TrimProjectName)
add_executable(trim-example-app main.cpp)
# Just running make will not install the executable.
# You need to run
#
# make install
#
# DESTINATION bin will be written to some system defined directory
# on MAC for me it ended up in
# -- Installing: /usr/local/bin/trim-example-app
install(TARGETS trim-example-app DESTINATION bin)
Using libraries with CMake
From Usage of Libraries with CMake
Go to text →
Links
Command in CMake
add_library
add_library(<name> [STATIC | SHARED | MODULE]
[EXCLUDE_FROM_ALL]
[<source>...])
Example CMakeLists.txt for library
CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(mymath)
add_library(mymath adder.cpp)
Working Example
Working Example
gt.sandbox.checkout.commit.cleanly 9671ea2 \
&& cd "$GT_SANDBOX_REPO/cpp/LibProducer" \
&& cmake -S . -B ./build \
&& cd build \
&& make \
&& cd "$GT_SANDBOX_REPO/cpp/LibConsumer" \
&& cmake -S . -B ./build \
&& cd build \
&& make \
&& echo "Running ./LibConsumer" \
&& ./LibConsumer
Output of building library
...
[ 50%] Building CXX object CMakeFiles/mymath.dir/adder.cpp.o
[100%] Linking CXX static library libmymath.a
[100%] Built target mymath
note lib
prefix is added and .a
suffix is added.
The default output of add_library
is static library of .a
.
.a static library will contain the definitions. But will still need the declaration (the header file).
Children
- CMakeLists.txt
- Examples
- Installing into User Level Bin Directory cmake install
- Linking Submodule Library
- Options
- Overriding Option
- Usage of Libraries with CMake
- add_subdirectory
Backlinks