make的link_directories命令不起作用

简介: 按照《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 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})

这下可以编译通过了。

目录
相关文章
|
1月前
|
测试技术 编译器 vr&ar
CMake深度解析:掌握add_custom_command,精通Makefile生成规则(一)
CMake深度解析:掌握add_custom_command,精通Makefile生成规则
68 1
|
3月前
|
Linux iOS开发 MacOS
pnpm全局安装报错:Run “pnpm setup“ to create it automatically, or set the global-bin-dir setting, or the PN
pnpm全局安装报错:Run “pnpm setup“ to create it automatically, or set the global-bin-dir setting, or the PN
511 0
|
10月前
CMake Error: The source “xxx“ does not match the source “yyy“ used to generate cache. Re-run cmake
CMake Error: The source “xxx“ does not match the source “yyy“ used to generate cache. Re-run cmake
405 0
|
1月前
|
存储 Linux C++
CMake深度解析:掌握add_custom_command,精通Makefile生成规则(二)
CMake深度解析:掌握add_custom_command,精通Makefile生成规则
30 0
|
1月前
|
存储 Shell iOS开发
CMake中文手册_target_link_libraries(3.26)
CMake中文手册_target_link_libraries(3.26)
27 0
|
1月前
|
Unix Linux Shell
CMake深度解析:掌握add_custom_command,精通Makefile生成规则(三)
CMake深度解析:掌握add_custom_command,精通Makefile生成规则
48 1
|
1月前
CMake中FindPackageHandleStandardArgs.cmake文件的作用和用法
CMake中FindPackageHandleStandardArgs.cmake文件的作用和用法
24 2
|
8月前
|
Ubuntu Linux
No manual entry for ls解决办法
No manual entry for ls解决办法
|
10月前
|
自然语言处理
openEuler 打开图形菜单报错:make [1]:‘ [ scripts/i Makefile. host:9: scripts/ kconfig/ lexer. lex.c]错误 127
openEuler 打开图形菜单报错:make [1]:‘ [ scripts/i Makefile. host:9: scripts/ kconfig/ lexer. lex.c]错误 127
199 0
|
缓存 前端开发
[Css 修改后 Google浏览器上无效果] 文件上有:Generated source files should not be edited 的警告
[Css 修改后 Google浏览器上无效果] 文件上有:Generated source files should not be edited 的警告
166 0
[Css 修改后 Google浏览器上无效果] 文件上有:Generated source files should not be edited 的警告