1. Introduction

CMake is a cross-platform build system generator. Projects specify their build process with platform-independent CMake listfiles included in each directory of a source tree with the name CMakeLists.txt. Users build a project by using CMake to generate a build system for a native tool on their platform.

2. Using CMake with executables

Lets assume we have a simple app with a single .c file.

We by creating a CMakeLists.txt file in the root of our project.

cmake_minimum_required(VERSION 2.8)
project(app_project)
add_executable(myapp main.c)
install(TARGETS myapp DESTINATION bin)
  • add_executable: defines our binary with all linked source files. (针对 c/cpp 文件,生成可执行 bin 文件)
  • install: tells cmake to install our binary into the bin directory of the install directory.

3. Building

CMake supports out-of-source builds — so all our compiled code goes into a directory separate to the sources.

To start a build we create a new folder:

mkdir _build
cd _build

And call cmake with the path to the project’s root (in this case the parent folder):

cmake ..

By default cmake will install our build into the system directories.

To define a custom install directory we simply pass -DCMAKE_INSTALL_PREFIX to cmake:

cmake .. -DCMAKE_INSTALL_PREFIX=../_install

To run the build script you can simply use the Makefile:

make
make install

We can now run our binary from the install directory:

../_install/bin/myapp

If we wanted to use a different generator we pass it to cmake using the -G parameter:

cmake .. -GXcode

4. Building library

Building our own library. Use add_library.

cmake_minimum_required(VERSION 2.8)
project(libtest_project)
add_library(test STATIC test.c)
install(TARGETS test DESTINATION lib)
install(FILES test.h DESTINATION include)

CMake will build the library as libtest.a and install it into lib folder of the install directory.

5. Reference

0 issues reported

results matching ""

    No results matching ""