cmake多目录编译

2021/10/30 23:09:47

本文主要是介绍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\n");
    printf("3 + 2 = %d\n", math_add(3, 2));
    printf("3 - 2 = %d\n", 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\n", 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\n", 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)  # 指定生成目录


这篇关于cmake多目录编译的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程