按照《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})
这下可以编译通过了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2015-08-17 Linux 多线程调试(内存占用、死循环、CPU占用率高……)
2015-08-17 Linux下如何查看高CPU占用率线程 LINUX CPU利用率计算
2015-08-17 嵌入式设备web服务器比较
2015-08-17 webserver/CGI
2015-08-17 CGI编程完全手册
2015-08-17 cgic: CGI的C函数库
2015-08-17 linux下的webserver BOA及CGIC库的使用指南(转帖)