当前位置:网站首页>cmake之add_dependencies
cmake之add_dependencies
2022-07-24 01:19:00 【大草原的小灰灰】
简介
add_dependencies(<target> [<target-dependency>]...)
- 官方文档对此的解释为 :在顶级目标之间添加依赖关系。使顶层 target 依赖于其他顶层目标,以确保它们在 target 之前构建。顶级目标是由 add_executable(),add_library() 或 add_custom_target() 命令之一创建的。
实例
目录结构
├── CMakeLists.txt
├── lib
│ ├── CMakeLists.txt
│ ├── mymath.cpp
│ └── mymath.h
└── src
├── CMakeLists.txt
└── main.cpp
最外层cmake
#指定cmake最低版本
cmake_minimum_required(VERSION 3.14)
#指定工程名
project(demo)
#添加子目录
add_subdirectory(${PROJECT_SOURCE_DIR}/src)
add_subdirectory(${PROJECT_SOURCE_DIR}/lib)
lib目录下cmake
#指定cmake最低版本
cmake_minimum_required(VERSION 3.14)
//指定库文件生成目录
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
//生成库文件
add_library(myMath SHARED ${PROJECT_SOURCE_DIR}/lib/mymath.cpp)
src目录下cmake
#指定cmake最低版本
cmake_minimum_required(VERSION 3.14)
#指定头文件位置
include_directories(${PROJECT_SOURCE_DIR}/lib)
#设置可执行文件生成目录
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
#生成可执行文件
add_executable(res ${PROJECT_SOURCE_DIR}/src/main.cpp)
#链接动态库
target_link_libraries(res ${PROJECT_BINARY_DIR}/bin/libmyMath.so)
测试
- 我们直接编译代码
[[email protected] build]# make
Scanning dependencies of target res
[ 25%] Building CXX object src/CMakeFiles/res.dir/main.cpp.o
make[2]: *** No rule to make target 'bin/libmyMath.so', needed by 'bin/res'. Stop.
make[1]: *** [CMakeFiles/Makefile2:91: src/CMakeFiles/res.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
[[email protected] build]#
- 可以看到会有报错,因为在最外层我们添加子目录时,先添加src子目录,也就是说我们在生成可执行文件res时,链接库文件的时候,库文件还没有生成。当然你可以调整添加子目录的顺序,先生成库文件,再生成可执行文件。但在实际开发中,链接库文件时,库文件还未生成的场景还是比较多的。
- 这个时候就可以使用add_dependencies命令。修改src目录下的cmake
#指定cmake最低版本
cmake_minimum_required(VERSION 3.14)
#指定头文件位置
include_directories(${PROJECT_SOURCE_DIR}/lib)
#设置可执行文件生成目录
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
#生成可执行文件
add_executable(res ${PROJECT_SOURCE_DIR}/src/main.cpp)
#指定依赖
add_dependencies(res myMath)
#链接动态库
target_link_libraries(res ${PROJECT_BINARY_DIR}/bin/libmyMath.so)
- 添加add_dependencies(res myMath),意思就是告诉编译器,我知道生成res程序要链接myMath这个库文件,但是现在我还没有这个库文件,你先生成res,我随后就把库文件myMath给你生成出来。
边栏推荐
- HCIP实验
- High voltage technology learning summary
- 对皮尔逊相关系数进行假设检验
- Sword *offer04 rebuild binary tree
- Use and understanding of string functions (2)
- Static extension configuration
- BAT代码:批量文件下划线重命名
- Bubble sort, quick sort
- Basic use of crawler requests module
- How safe is Volvo XC90? 5 seats and 7 seats are available
猜你喜欢
随机推荐
Polymer synthesis technology
HCIP第一天笔记
Group chat room based on UDP
Source code installation and use of APIs IX
docker redis
深入理解协程
High voltage technical examination questions with answers
C language: student management system based on sequence table, super detailed, all with notes, if you don't understand it after reading, please slap me.
数字化转型时代的企业数据新基建 | 爱分析报告
Explanation and induction of polymer physics terms
Sublime text 3 Chinese + add common plug-ins
Hcip day 6_ Comprehensive experiment in special areas
RIP---路由信息协议
HCIP第十一天笔记
Bat Code: batch file underline rename
1000个Okaleido Tiger首发上线Binance NFT,引发抢购热潮
SQL CASE 多条件用法
Use of crawler request library 2
OSPF experiment
Answer to the short answer question of polymer synthesis technology









