CMake learning notes

error: /usr/bin/ld: cannot find -lhello

solution:

make的link_directories命令不起作用

https://www.cnblogs.com/lidabo/p/7380158.html

按照《CMake Practice》中第六章的设置,采用include_directories命令去寻找共享库的路径,src/CMakeLists.txt如下:

ADD_EXECUTABLE(main main.c)
INCLUDE_DIRECTORIES(/tmp/include/hello)
LINK_DIRECTORIES(/tmp/lib/)
TARGET_LINK_LIBRARIES(main libhello.a)

执行cmake及make后,仍然有link error。

cmake的官网对include_directories的说明如下:

*Specify directories in which the linker will look for libraries.

link_directories(directory1 directory2 …)

Note that this command is rarely necessary. Library locations returned by find_package() and find_library() are absolute paths. Pass these absolute library file paths directly to the target_link_libraries() command. CMake will ensure the linker finds them.*

官网不推荐使用link_directoris,而是推荐使用find_package和find_library寻找共享库的绝对路径,再传给target_link_libraries使用。

按照这里的例子,改写了src/CMakeLists.txt如下:

ADD_EXECUTABLE(main main.c)
INCLUDE_DIRECTORIES(/tmp/include/hello)

find_library(LIBHELLO_PATH hello /tmp/lib)
IF(NOT LIBHELLO_PATH) MESSAGE(FATAL_ERROR "libhello not found") ENDIF(NOT LIBHELLO_PATH) MESSAGE(STATUS ${LIBHELLO_PATH} " found") TARGET_LINK_LIBRARIES(main ${LIBHELLO_PATH})

这下可以编译通过了。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值