强制CMake使用完整的库路径 [英] Force CMake to use the full library path

查看:860
本文介绍了强制CMake使用完整的库路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题。我在我的Linux机器上有一个单独的{bin,lib,include}树,其中安装了CMake和我开发工作所需的所有库。但只有PATH环境变量设置为此bin目录,并且由于几个原因,我不能设置LD_LIBRARY_PATH。此树内的所有程序都使用RPATH构建。我使用的CMake 3.3.1也在这棵树里面。



现在我想使用libcurl编译程序并设置以下CMakeLists.txt的问题

 项目(示例)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
SET(CMAKE_SKIP_BUILD_RPATH FALSE)
FIND_PACKAGE(需要CURL)
FIND_PACKAGE(OpenSSL REQUIRED)
INCLUDE_DIRECTORIES($ {CURL_INCLUDE_DIR} $ {OPENSSL_INCLUDE_DIR})
SET(LIBS $ {CURL_LIBRARIES} $ {OPENSSL_LIBRARIES})

ADD_EXECUTABLE(curl_ex src / curl_ex.c)
TARGET_LINK_LIBRARIES(curl_ex $ {LIBS})

当我现在运行CMake时,curl和OpenSSL设置我的个人软件树被找到,由于它存在于与CMake相同的前缀内。



但是当我使用 make VERBOSE = 1 构建项目时,我看到以下链接命令:

  gcc CMakeFiles / curl_ex.dir / src / curl_ex.co -o curl_ex -rdynamic -lcurl -lssl -lcrypto 

,构建可执行文件指的是系统范围内安装的curl和openssl库,而不是配置期间找到的一个cmake。



如何强制CMake使用它在执行链接时找到的库?

解决方案

将我的评论转换为答案



我能够重现你的问题 - 即使没有完全相同的环境,并找到两个可能的解决方案:


  1. 您设置政策 CMP0060 NEW

      cmake_policy(SET CMP0060 NEW)




    此策略的新行为是通过完整路径链接库,即使



  2. 您可以创建一个中间导入的库,并使用 IMPORTED_LOCATION (请参阅 [CMake] TARGET_LINK_LIBRARIES完整路径库

      add_library(curl未知导入)
    set_property(TARGET curl PROPERTY IMPORTED_LOCATION$ {CURL_LIBRARIES})
    target_link_libraries(curl_ex curl)

    这对我有用,但根据导入库行为,您可能还需要设置 IMPORTED_IMPLIB

  3. 请检查 CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES 的设置,因为其中列出的路径被视为隐性搜索路径,并相应替换完整库路径(请参阅 cmComputeLinkInformation :: CheckImplicitDirItem() UnixPaths.cmake

     讯息(CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES:$ {CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES})


    I have the following problem. I have a separate {bin,lib,include} tree on my Linux machine where CMake and all my libraries I need for my development work are installed. But only the PATH environment variable is set to this bin directory and for several reason I can not set the LD_LIBRARY_PATH. All programs inside this tree are build using RPATHs. The CMake 3.3.1 I am using is also inside this tree.

    Now the problem I want to compile a program using libcurl and set up the following CMakeLists.txt

    PROJECT(EXAMPLE)
    CMAKE_MINIMUM_REQUIRED(VERSION 2.8) 
    SET(CMAKE_SKIP_BUILD_RPATH FALSE)
    FIND_PACKAGE(CURL REQUIRED)
    FIND_PACKAGE(OpenSSL REQUIRED) 
    INCLUDE_DIRECTORIES(${CURL_INCLUDE_DIR} ${OPENSSL_INCLUDE_DIR})
    SET(LIBS ${CURL_LIBRARIES} ${OPENSSL_LIBRARIES}) 
    
    ADD_EXECUTABLE(curl_ex src/curl_ex.c)
    TARGET_LINK_LIBRARIES(curl_ex ${LIBS}) 
    

    When I now run CMake the curl and the OpenSSL setup out of my personal software tree is found due to the fact that it resides inside the same prefix as CMake.

    But when I build the project using make VERBOSE=1 I see the following linking command:

    gcc CMakeFiles/curl_ex.dir/src/curl_ex.c.o  -o curl_ex -rdynamic -lcurl -lssl -lcrypto 
    

    and the build executable refers to the system-wide installed curl and openssl libraries instead of the one cmake found during the configuration.

    How can I force CMake to use the libraries it found when it performs the linking?

    解决方案

    Turning my comments into an answer

    I was able to reproduce your problem - even without having the exact same environment - and found two possible solutions:

    1. You set policy CMP0060 to NEW

      cmake_policy(SET CMP0060 NEW)
      

      The NEW behavior for this policy is to link libraries by full path even if they are in implicit link directories.

    2. You can create a intermediate imported library and use IMPORTED_LOCATION (see [CMake] TARGET_LINK_LIBRARIES with full path libraries)

      add_library(curl UNKNOWN IMPORTED)
      set_property(TARGET curl PROPERTY IMPORTED_LOCATION "${CURL_LIBRARIES}")
      target_link_libraries(curl_ex curl)
      

      This worked for me, but according to CMake imported library behaviour you may need to also set IMPORTED_IMPLIB.

    Background

    Please check the setting of CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES because the paths listed there are taken as "implicit" search paths and full library paths are replaced accordingly (see cmComputeLinkInformation::CheckImplicitDirItem() and UnixPaths.cmake)

    message("CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES: ${CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES}")
    

    这篇关于强制CMake使用完整的库路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆