CMake Tutorial1_1 最简单的例子

一 内容
  1. CMake最简单的例子只需要一个3行的CMakeLists.txt和一个源文件例如main.cc

  2. CMakeLists.txt

    # 设置CMake最低版本
    cmake_minimum_required(VERSION 3.16)
    
    # 设置项目名称及版本
    project(Tutorial VERSION 1.0)
    # 可仅设置项目名称
    # project(Tutorial)
    
    # 生成可执行文件
    add_executable(Tutorial main.cc)
    
  3. main.cc

    #include <iostream>
    
    int main() {
      std::cout << "first case." << std::endl;
      return 0;
    }
    
二 构建
  1. 创建并进入构建目录。因为构建会生成其他文件,单独的构建目录可以避免源文件目录被污染。

    lee@leedeMacBook-Pro step1 % ls -l
    total 16
    -rw-r--r--  1 lee  staff  228  5 27 17:03 CMakeLists.txt
    -rw-r--r--  1 lee  staff   90  5 27 15:44 main.cc
    lee@leedeMacBook-Pro step1 % mkdir build
    lee@leedeMacBook-Pro step1 % ls -l
    total 16
    -rw-r--r--  1 lee  staff  228  5 27 17:03 CMakeLists.txt
    drwxr-xr-x  2 lee  staff   64  5 27 19:32 build
    -rw-r--r--  1 lee  staff   90  5 27 15:44 main.cc
    lee@leedeMacBook-Pro step1 % cd build
    lee@leedeMacBook-Pro build % 
    
  2. build&run

    lee@leedeMacBook-Pro build % cmake ..
    -- The C compiler identification is AppleClang 12.0.0.12000032
    -- The CXX compiler identification is AppleClang 12.0.0.12000032
    -- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
    -- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
    -- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/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: /Users/lee/research/cmake_learn/cmake_tutorial/step1/build
    lee@leedeMacBook-Pro build % ls
    CMakeCache.txt		Makefile
    CMakeFiles		cmake_install.cmake
    lee@leedeMacBook-Pro build % cmake --build .
    Scanning dependencies of target Tutorial
    [ 50%] Building CXX object CMakeFiles/Tutorial.dir/main.cc.o
    [100%] Linking CXX executable Tutorial
    [100%] Built target Tutorial
    lee@leedeMacBook-Pro build % ls
    CMakeCache.txt		Makefile		cmake_install.cmake
    CMakeFiles		Tutorial
    lee@leedeMacBook-Pro build % ./Tutorial 
    first case.
    lee@leedeMacBook-Pro build % 
    
三 Github
四 参考
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值