• CMake 基础学习


    CMake 简介

    CMake 是一个跨平台、开源的构建系统,一个集软件构建、测试、打包于一身的软件。它使用与平台和编译器独立的配置文件来对软件编译过程进行控制。

    一 初识CMake

    使用一个十分简单的例子:
    在新建的项目文件夹下,建立如下两个文件

    $ tree
    .
    ├── CMakeLists.txt
    └── main.cpp
    
    • 1
    • 2
    • 3
    • 4

    CMakelists.txt 中的文件内容如下

    # Set the minimum version of CMake that can be used
    # To find the cmake version run
    # $ cmake --version
    cmake_minimum_required(VERSION 3.5)
    
    # Set the project name
    project (hello_cmake)
    
    # Add an executable
    add_executable(hello_cmake main.cpp)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    main.cpp 中是一个简单的C++程序

    #include
    using namespace std;
    int main(){
        cout <<" hello word !"<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    关于CMakelists.txt 中的内容后面再解释,我们先将整个程序编译运行出来。

    二进制目录

    运行cmake命令 的根文件夹或者顶级文件夹称为 CMAKE_BINARY_DIR,是所有二进制文件的根文件夹。CMake 及支持就地构建和生成二进制文件,也支持在源代码外构建和生成二进制文件。

    就地构建

    就地构建会在源代码所在的文件夹生成所有的临时文件,使得源代码和临时文件都混在一起,这样会很不方便。
    我们执行 cmake . 命令, 注意符号 “ . ” 表示在当前目录执行。

    $ cmake .
    -- The C compiler identification is GNU 7.3.0
    -- The CXX compiler identification is GNU 7.3.0
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Check for working C compiler: /home/panlichen/miniconda3/envs/oneflow-dev-gcc7-v2/bin/x86_64-conda_cos6-linux-gnu-cc - skipped
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Check for working CXX compiler: /home/panlichen/miniconda3/envs/oneflow-dev-gcc7-v2/bin/x86_64-conda_cos6-linux-gnu-c++ - skipped
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/panlichen/zrk/hello_cmake
    
    $ tree
    .
    ├── CMakeCache.txt
    ├── CMakeFiles
    │   ├── 3.18.2
    │   │   ├── CMakeCCompiler.cmake
    │   │   ├── CMakeCXXCompiler.cmake
    │   │   ├── CMakeDetermineCompilerABI_C.bin
    │   │   ├── CMakeDetermineCompilerABI_CXX.bin
    │   │   ├── CMakeSystem.cmake
    │   │   ├── CompilerIdC
    │   │   │   ├── a.out
    │   │   │   ├── CMakeCCompilerId.c
    │   │   │   └── tmp
    │   │   └── CompilerIdCXX
    │   │       ├── a.out
    │   │       ├── CMakeCXXCompilerId.cpp
    │   │       └── tmp
    │   ├── cmake.check_cache
    │   ├── CMakeDirectoryInformation.cmake
    │   ├── CMakeOutput.log
    │   ├── CMakeTmp
    │   ├── hello_cmake.dir
    │   │   ├── build.make
    │   │   ├── cmake_clean.cmake
    │   │   ├── DependInfo.cmake
    │   │   ├── depend.make
    │   │   ├── flags.make
    │   │   ├── link.txt
    │   │   └── progress.make
    │   ├── Makefile2
    │   ├── Makefile.cmake
    │   ├── progress.marks
    │   └── TargetDirectories.txt
    ├── cmake_install.cmake
    ├── CMakeLists.txt
    ├── main.cpp
    └── Makefile
    
    8 directories, 28 files
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    可见已经成功生成了makefile文件,接下来我们make一下,并执行

    $ make
    $ ./hello_cmake
     hello word !
    
    • 1
    • 2
    • 3

    当修改代码时,没有增减源文件修改CMakeLists.txt 只需要重新make

    源外构建 (推荐)

    使用源外构建,你可以创建单个生成文件夹,该文件夹可以位于文件系统的任何位置。所有临时构建的目标文件都位于此目录中,以保持源码目录结构的整洁。

    我们创建一个build 文件夹,在build 文件夹下执行 命令:
    cmake + CMakeLists.txt 所在路径

    $ tree ..
    ..
    ├── build
    ├── CMakeLists.txt
    └── main.cpp
    
    
    $ cmake .. 
    -- The C compiler identification is GNU 7.3.0
    -- The CXX compiler identification is GNU 7.3.0
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Check for working C compiler: /home/panlichen/miniconda3/envs/oneflow-dev-gcc7-v2/bin/x86_64-conda_cos6-linux-gnu-cc - skipped
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Check for working CXX compiler: /home/panlichen/miniconda3/envs/oneflow-dev-gcc7-v2/bin/x86_64-conda_cos6-linux-gnu-c++ - skipped
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/panlichen/zrk/hello_cmake/build
    
    $ make
    Scanning dependencies of target hello_cmake
    [ 50%] Building CXX object CMakeFiles/hello_cmake.dir/main.cpp.o
    [100%] Linking CXX executable hello_cmake
    [100%] Built target hello_cmake
    
    $ ./hello_cmake 
     hello word !
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    源外构建后,目录结构如下

    $ tree ..
    ..
    ├── build
    │   ├── CMakeCache.txt
    │   ├── CMakeFiles
    │   │   ├── 3.18.2
    │   │   │   ├── CMakeCCompiler.cmake
    │   │   │   ├── CMakeCXXCompiler.cmake
    │   │   │   ├── CMakeDetermineCompilerABI_C.bin
    │   │   │   ├── CMakeDetermineCompilerABI_CXX.bin
    │   │   │   ├── CMakeSystem.cmake
    │   │   │   ├── CompilerIdC
    │   │   │   │   ├── a.out
    │   │   │   │   ├── CMakeCCompilerId.c
    │   │   │   │   └── tmp
    │   │   │   └── CompilerIdCXX
    │   │   │       ├── a.out
    │   │   │       ├── CMakeCXXCompilerId.cpp
    │   │   │       └── tmp
    │   │   ├── cmake.check_cache
    │   │   ├── CMakeDirectoryInformation.cmake
    │   │   ├── CMakeOutput.log
    │   │   ├── CMakeTmp
    │   │   ├── hello_cmake.dir
    │   │   │   ├── build.make
    │   │   │   ├── cmake_clean.cmake
    │   │   │   ├── CXX.includecache
    │   │   │   ├── DependInfo.cmake
    │   │   │   ├── depend.internal
    │   │   │   ├── depend.make
    │   │   │   ├── flags.make
    │   │   │   ├── link.txt
    │   │   │   ├── main.cpp.o
    │   │   │   └── progress.make
    │   │   ├── Makefile2
    │   │   ├── Makefile.cmake
    │   │   ├── progress.marks
    │   │   └── TargetDirectories.txt
    │   ├── cmake_install.cmake
    │   ├── hello_cmake
    │   └── Makefile
    ├── CMakeLists.txt
    └── main.cpp
    
    9 directories, 32 files
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
  • 相关阅读:
    SpringSecurity原理解析以及CSRF跨站请求伪造攻击
    [PyTorch][chapter 53][Auto Encoder 实战]
    正确理解redux Toolkits中createSlice的action.payload
    【Python高级编程】Matplotlib 绘图中文显示问题与常见错误合集
    Vscode 设置clang-format
    Yolov8小目标检测(19):动态蛇形卷积(Dynamic Snake Convolution),增强细长微弱特征 | ICCV2023
    MySQL篇-MySQL存储引擎详解
    StarRocks简介及安装
    KNN(K近邻)水仙花的分类(含答案)
    解决idea运行maven项目报错Unresolved plugin ‘org.apache.maven.pluginsxxxx
  • 原文地址:https://blog.csdn.net/greatcoder/article/details/126011959