target_link_libraries:该函数声明了链接时需要参与的依赖库名称或target。其名称可包含完整路径。
命名的
target_link_libraries命令的格式:
target_link_libraries(<target> ... <item>... ...)
- target_link_libraries(<target>
- <PRIVATE|PUBLIC|INTERFACE> <item>...
- [<PRIVATE|PUBLIC|INTERFACE> <item>...]...)
例如:
target_link_libraries(sample PUBLIC CURL::curl glib m)
注意:由于一个target中包含多个属性,一般情况下包含了头文件路径。所以使用target作为参数传入此函数时,无需调用 target_include_directories 再次声明添加头文件路径。
- target_link_libraries(PUBLIC target target1 target2)
- target_link_libraries(PUBLIC target3 target target4)
- target_link_libraries(PRIVATE target target1 target2)
- target_link_libraries(PRIVATE target3 target target4)
- target_link_libraries(INTERFACE target target1 target2)
- target_link_libraries(INTERFACE target3 target target4)
经常的使用方式是
target_link_libraries(target Third_part_lib)
Third_part_lib参数,每个第三库会进行定义,具体的名称可以找到库的说明文档进行查看。
target_link_libraries(target ${OpenCV_LIBS})
- target1:
- #include “opencv”
- cv::imwait(0);
-
- target2:
- #include “opencv”
- cv::imwait(0);
- cv::imwrite();
-
- target4:
- #include “opencv”
- cv::imwait(0);
- cv::imwrite();
-
- target_link_libraries(target1 OpenCV)
- target_link_libraries(target target1 target2)
- target_link_libraries(target3 target target4)
target2和target4能使用imwait(0),但是不能使用imwrite(),除非在target2和target4的CMakeLists中添加target_link_libraries(target2/4 OpenCV);