这两天学习一下CMake的一点语法,为了以后能使用CMake跨平台构建项目。但是在为target添加共享库的时候,即使我在CMakeLists.txt中使用了LINK_DIRECTORIES()TARGET_LINK_LIBRARIES()

1
2
3
4
5
6
7
ADD_EXECUTABLE(hellomain main.c)
SET(EXECUTABLE_OUTPUT_PATH ${HELLOMAIN_BINARY_DIR}/bin)

INCLUDE_DIRECTORIES(BEFORE /mnt/hgfs/CentOS_code/cmake_practice/t4/zjshao/include/hello)
LINK_DIRECTORIES(/mnt/hgfs/CentOS_code/cmake_practice/t4/zjshao/lib)

TARGET_LINK_LIBRARIES(hellomain libhello.so)

但是在make的时候还是很出现了
1
/usr/bin/ld: cannot find -lhello

在看过别人的CMakeLists.txt之后,才知道是我cmake指令的顺序有问题,改成如下这样便可以找到链接库了:

1
2
3
4
5
6
7
8
SET(EXECUTABLE_OUTPUT_PATH ${HELLOMAIN_BINARY_DIR}/bin)

INCLUDE_DIRECTORIES(BEFORE /mnt/hgfs/CentOS_code/cmake_practice/t4/zjshao/include/hello)
LINK_DIRECTORIES(/mnt/hgfs/CentOS_code/cmake_practice/t4/zjshao/lib)

ADD_EXECUTABLE(hellomain main.c)

TARGET_LINK_LIBRARIES(hellomain libhello.so)

也就是说在ADD_EXECUTABLE()之前就要将LINK_DIRECTORIESINCLUDE_DIRECTORIES设置好。

Comments

2016-03-27