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