有了链接库后要在源码中添加,使用target_link_libraries语句。
target_link_libraries(<target> ... <item>... ...)
target 是要添加链接库的目标,并且必须由 add_executable() 或 add_library() 创建,并且一定不能成为 ALIAS目标。
item 是要链接的库,可以是库的名字,库的完整(绝对)路径。
main.c
#include <stdio.h>
int main()
{
hello();
return 0;
}
CMakeLists.txt
使用CMake学习(二) ——生成链接库生成的链接库
链接库的源码 hello.c 为:
#include <stdio.h>
int hello()
{
printf("hello CMake!\n");
return 0;
}
CMakeLists.txt内容:
# CMake最低版本要求
cmake_minimum_required(VERSION 3.5)
# 项目名称
project(test_3)
# 指定生成目标
add_executable(test_3 main.c)
# 添加链接库
# 这里要写库文件的绝对路径
target_link_libraries(test_3 /home/vistar/desktop/CMake/CMake_2/build/libtest_2.a)
运行结果为: