zoukankan      html  css  js  c++  java
  • cmake多目录构建项目

    普通多目录

    源码

    ..
    ├── build
    ├── CMakeLists.txt
    ├── main.c
    └── math
        ├── CMakeLists.txt
        ├── math_add.c
        ├── math.h
        └── math_sub.c
    
    2 directories, 6 files
    
    
    // math/math.h
    #if !defined(__MATH_H__)
    #define __MATH_H__
    
    int math_add(int a, int b);
    int math_sub(int a, int b);
    
    #endif // __MATH_H__
    
    
    // math/math_add.c
    #include "math.h"
    
    int math_add(int a, int b) {
        return a + b;
    }
    
    
    // math/math_sub.c
    #include "math.h"
    
    int math_sub(int a, int b) {
        return a - b;
    }
    
    
    # math/CMakeLists.txt
    aux_source_directory(./ LIST_MATH)
    
    add_library(lib_math ${LIST_MATH})
    
    
    // main.c
    #include <stdio.h>
    
    #include "math.h"
    
    int main(int argc, char **argv){
        printf("Hello World from t1 Main
    ");
        printf("3 + 2 = %d
    ", math_add(3, 2));
        printf("3 - 2 = %d
    ", math_sub(3, 2));
        return 0;
    }
    
    
    # CMakeLists.txt
    cmake_minimum_required(VERSION 3.11)
    
    project(main)
    
    add_subdirectory(./math)
    
    set(SRC_MAIN main.c)
    
    add_executable(main ${SRC_MAIN})
    
    target_link_libraries(main lib_math)
    
    

    运行

    fl@fl:~/Qt/test_code/cmake/t11/build$ cmake ..
    -- The C compiler identification is GNU 9.3.0
    -- The CXX compiler identification is GNU 9.3.0
    -- Check for working C compiler: /usr/bin/cc
    -- Check for working C compiler: /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: /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/fl/Qt/test_code/cmake/t11/build
    fl@fl:~/Qt/test_code/cmake/t11/build$ make
    Scanning dependencies of target lib_math
    [ 20%] Building C object math/CMakeFiles/lib_math.dir/math_add.c.o
    [ 40%] Building C object math/CMakeFiles/lib_math.dir/math_sub.c.o
    [ 60%] Linking C static library liblib_math.a
    [ 60%] Built target lib_math
    Scanning dependencies of target main
    [ 80%] Building C object CMakeFiles/main.dir/main.c.o
    /home/fl/Qt/test_code/cmake/t11/main.c: In function ‘main’:
    /home/fl/Qt/test_code/cmake/t11/main.c:7:28: warning: implicit declaration of function ‘math_add’ [-Wimplicit-function-declaration]
        7 |     printf("3 + 2 = %d
    ", math_add(3, 2));
          |                            ^~~~~~~~
    /home/fl/Qt/test_code/cmake/t11/main.c:8:28: warning: implicit declaration of function ‘math_sub’ [-Wimplicit-function-declaration]
        8 |     printf("3 - 2 = %d
    ", math_sub(3, 2));
          |                            ^~~~~~~~
    [100%] Linking C executable main
    [100%] Built target main
    fl@fl:~/Qt/test_code/cmake/t11/build$ ll
    total 64
    drwxrwxr-x 4 fl fl  4096 10月 30 21:57 ./
    drwxrwxr-x 5 fl fl  4096 10月 30 20:41 ../
    -rw-rw-r-- 1 fl fl 13725 10月 30 21:57 CMakeCache.txt
    drwxrwxr-x 5 fl fl  4096 10月 30 21:57 CMakeFiles/
    -rw-rw-r-- 1 fl fl  1687 10月 30 21:57 cmake_install.cmake
    -rwxrwxr-x 1 fl fl 16872 10月 30 21:57 main*
    -rw-rw-r-- 1 fl fl  5134 10月 30 21:57 Makefile
    drwxrwxr-x 3 fl fl  4096 10月 30 21:57 math/
    
    
    

    注意事项

    1. add_library(<name> [STATIC | SHARED | MODULE] [EXCLUDE_FROM_ALL] [source1] [source2 ...]) # 使用指定的源文件生成库文件
      1. name对应于逻辑目标名称,并且必须在全局范围内唯一,构建的库的实际文件是基于本机平台的约定libmath.a(linux),math.lib(win32)
      2. 可以指定生成库的lwxk
        1. STATIC库是目标文件的档案,用于链接其他目标
        2. SHARED库运行时加载
        3. MODULE
      3. 默认是STATIC活着SHARED取决于当前变量的BUILD_SHARED_LIBS的ON值
      4. 默认情况下,库文件将在构建树目录中创建对应于命令所在的源树目录调用,通过set可改变。
        1. ARCHIVE_OUTPUT_DIRECTORY
        2. LIBRARY_OUTPUT_DIRECTORY
        3. RUNTIME_OUTPUT_DIRECTORY
      5. add_library的源参数可以使用生成表达式。
    2. add_subdirectory(source_dir [binary_dir] [EXCLUDE_FROM_ALL]) # 将子目录添加到构建工程中
      1. source_dir指定CMakeLists.txt和代码文件的目录关系
      2. binary_dir指定要执行的目录放置位置。如果不录入默认同source_dir
    3. target_link_libraries(<target> ... <item>... ...) # 指定链接目标或依赖
      1. target必须是add_executable活着add_library生成的目标。
      2. item类型可能为:
        1. A library target name: math.so
        2. A full path to a library file: /usr/lib/math.so
        3. A plain library name: math
        4. A link flag
        5. A generator expression
      3. 这里有一个依赖顺序问题(TODO)。。。

    标准多目录 + 指定输出目录

    源码

    ..
    ├── build
    ├── CMakeLists.txt
    ├── math
    │   ├── CMakeLists.txt
    │   ├── math_add.c
    │   ├── math.h
    │   └── math_sub.c
    └── src
        ├── CMakeLists.txt
        └── main.c
    
    3 directories, 7 files
    
    
    # math/CMakeLists.txt
    
    aux_source_directory(./ LIST_MATH)
    
    add_library(lib_math ${LIST_MATH})
    
    
    # src/CMakeLists.txt
    include_directories(${PROJECT_SOURCE_DIR}/math)
    
    set(SRC_MAIN main.c)
    
    add_executable(main ${SRC_MAIN})
    
    target_link_libraries(main lib_math)
    
    
    # CMakeLists.txt
    cmake_minimum_required(VERSION 3.11)
    
    project(main)
    
    add_subdirectory(./math lib) # 指定生成目录
    
    add_subdirectory(./src bin)  # 指定生成目录
    

    引用多个库

    源码

    ..
    ├── 3rd
    │   ├── math
    │   │   ├── CMakeLists.txt
    │   │   ├── math_add.c
    │   │   ├── math.h
    │   │   └── math_sub.c
    │   └── print
    │       ├── CMakeLists.txt
    │       ├── print.c
    │       └── print.h
    ├── build
    ├── CMakeLists.txt
    └── src
        ├── CMakeLists.txt
        └── main.c
    
    5 directories, 10 files
    
    # CMakeLists.txt
    cmake_minimum_required(VERSION 3.11)
    
    project(main)
    
    add_subdirectory(./src bin)
    add_subdirectory(./3rd/math)
    add_subdirectory(./3rd/print)
    
    
    # src/CMakeLists.txt
    include_directories(${PROJECT_SOURCE_DIR}/3rd)
    
    set(SRC_MAIN main.c)
    
    add_executable(main ${SRC_MAIN})
    
    target_link_libraries(main lib_math)
    target_link_libraries(main lib_print)
    
    
    # 3rd/math/CMaktLists.txt
    aux_source_directory(./ LIST_MATH)
    
    add_library(lib_math ${LIST_MATH})
    
    
    ## 3rd/print/CMakeLists.txt
    aux_source_directory(./ LIST_PRINT)
    
    add_library(lib_print ${LIST_PRINT})
    

    本文来自博客园,作者:faithlocus,转载请注明原文链接:https://www.cnblogs.com/faithlocus/p/15487795.html

  • 相关阅读:
    jQuery入门教程
    vue-lazyload 图片不更新
    Eggjs 设置跨域请求
    Vue.js错误: Maximum call stack size exceeded
    ubuntu nginx ssl 证书配置
    ubuntu 安装nginx, 出现 Unable to locate package
    nginx 判断移动端或者PC端 进入不同域名
    node.js 生成二维码
    Linux 配置ssh 免密码登录
    nodejs 从部署到域名访问
  • 原文地址:https://www.cnblogs.com/faithlocus/p/15487795.html
Copyright © 2011-2022 走看看