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/
注意事项
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) # 指定生成目录
这篇关于cmake多目录编译的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23增量更新怎么做?-icode9专业技术文章分享
- 2024-11-23压缩包加密方案有哪些?-icode9专业技术文章分享
- 2024-11-23用shell怎么写一个开机时自动同步远程仓库的代码?-icode9专业技术文章分享
- 2024-11-23webman可以同步自己的仓库吗?-icode9专业技术文章分享
- 2024-11-23在 Webman 中怎么判断是否有某命令进程正在运行?-icode9专业技术文章分享
- 2024-11-23如何重置new Swiper?-icode9专业技术文章分享
- 2024-11-23oss直传有什么好处?-icode9专业技术文章分享
- 2024-11-23如何将oss直传封装成一个组件在其他页面调用时都可以使用?-icode9专业技术文章分享
- 2024-11-23怎么使用laravel 11在代码里获取路由列表?-icode9专业技术文章分享
- 2024-11-22怎么实现ansible playbook 备份代码中命名包含时间戳功能?-icode9专业技术文章分享