一步一步学CMake 之 配置和构建C++ 工程

内容包含:

1. 准备源文件

2. 编写CMakeLists.txt文件

3. CMake构建

4. 中间生成文件


一步一步学 CMake 系列文章

第一篇:小试牛刀

1. 准备源文件

#include <cstdlib>
#include <iostream>
#include <string>

std::string say_hello() 
{ 
 return std::string("Hello, CMake world!"); 
}

int main() {
  std::cout << say_hello() << std::endl;
  return 0;
}

2. 编写CMakeLists.txt文件

# set minimum cmake version
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

# project name and language
project(hello-world LANGUAGES CXX)

add_executable(hello-world hello-world.cpp)

3. CMake构建

mkdir -p build
cd build
cmake ..

    -- The CXX compiler identification is GNU 5.4.0
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/xwang/example/build


cmake	--build	.

    Scanning dependencies of target hello-world
    [ 50%] Building CXX object CMakeFiles/hello-world.dir/hello-world.cpp.o
    [100%] Linking CXX executable hello-world
    [100%] Built target hello-worl

在GNU/Linux,    CMake默认生成Makefiles来构建项目

4. 中间生成文件

Makefile:  包含make指令将会运行的一系列指令集。
CMakeFiles :    包含临时文件的文件夹,CMake用于检测操作系统、编译器等的类型。另外, 它还包含特定于项目的文件。
cmake_install.cmake :   这是一个CMake脚本,用于处理安装的规则,该脚本只会在安装阶段使用。
CMakeCache.txt :    缓存文件,该文件会在再次运行配置是使用。

猜你喜欢

转载自blog.csdn.net/wanzew/article/details/104100294