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})

这下可以编译通过了。

目录
相关文章
|
Linux
CentOS7下搭建Jellyfin个人流媒体服务器
CentOS7下搭建Jellyfin个人流媒体服务器
1936 0
CentOS7下搭建Jellyfin个人流媒体服务器
|
人工智能 机器人 测试技术
【CMake报错】Cannot specify compile definitions for target “PRIVATE“ which is not built...
【CMake报错】Cannot specify compile definitions for target “PRIVATE“ which is not built...
|
Android开发
KernelSU基于内核的 SU。它通过自定义内核,直接在内核中赋予目标进程 root 权限。
KernelSU基于内核的 SU。它通过自定义内核,直接在内核中赋予目标进程 root 权限。
4904 0
|
8月前
|
运维 Cloud Native 应用服务中间件
阿里云微服务引擎 MSE 及 API 网关 2025 年 7 月产品动态
阿里云微服务引擎 MSE 面向业界主流开源微服务项目, 提供注册配置中心和分布式协调(原生支持 Nacos/ZooKeeper/Eureka )、云原生网关(原生支持Higress/Nginx/Envoy,遵循Ingress标准)、微服务治理(原生支持 Spring Cloud/Dubbo/Sentinel,遵循 OpenSergo 服务治理规范)能力。API 网关 (API Gateway),提供 APl 托管服务,覆盖设计、开发、测试、发布、售卖、运维监测、安全管控、下线等 API 生命周期阶段。帮助您快速构建以 API 为核心的系统架构.满足新技术引入、系统集成、业务中台等诸多场景需要。
|
缓存 网络协议 算法
Golang简单实现 分布式缓存+一致性哈希+节点再平衡(gossip + consistent + rebalance)
Golang简单实现 分布式缓存+一致性哈希+节点再平衡(gossip + consistent + rebalance)
522 0
|
Ubuntu Linux Docker
Win11彻底卸载WSL2系统(去除导航窗格Linux图标)
Win11彻底卸载WSL2系统(去除导航窗格Linux图标)
17905 51
UE4动画蓝图节点Layered blend per bone详解
UE4动画蓝图节点Layered blend per bone详解
819 1
|
存储 安全
atomic_int
atomic_int
759 0
|
存储 缓存 算法
深入了解Memcached:缓存技术的利器
Memcached是一个开源的高性能分布式内存缓存系统,用于加速动态Web应用。它通过将数据库查询结果、API调用结果或其他数据缓存到内存中,减少对数据库的访问频率,从而提高应用的响应速度。本文详细介绍了Memcached的基本原理、架构、安装配置、使用方法、测试方法以及应用场景。通过Memcached,开发者可以有效提升Web应用的性能,减少数据库负载,改善用户体验。
666 5
|
算法 C# 图形学
用unity先来个一副牌的单机斗地主
用unity先来个一副牌的单机斗地主

热门文章

最新文章