一步一步学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 :    缓存文件,该文件会在再次运行配置是使用。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值