Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
cmake:“;“使安装”;不链接Ubuntu中的库_Ubuntu_Cmake_Gnu Make - Fatal编程技术网

cmake:“;“使安装”;不链接Ubuntu中的库

cmake:“;“使安装”;不链接Ubuntu中的库,ubuntu,cmake,gnu-make,Ubuntu,Cmake,Gnu Make,我对CMake比较陌生,我开发了一个小项目,它构建了一个链接到共享库的库,名为external\u library。我的CMakeLists.txt文件如下所示: cmake_minimum_required(VERSION 2.8.12) project(project_name) include_directories(path_to_external_library_source_code) add_subdirectory(path_to_external_library_header

我对
CMake
比较陌生,我开发了一个小项目,它构建了一个链接到共享库的库,名为
external\u library
。我的
CMakeLists.txt
文件如下所示:

cmake_minimum_required(VERSION 2.8.12)
project(project_name)

include_directories(path_to_external_library_source_code)
add_subdirectory(path_to_external_library_header_files subproject/external_library)

target_link_libraries(project_name external_library)
install(TARGETS project_name DESTINATION installation_path)
cmake_minimum_required(VERSION 2.8.12)
project(project_name)

include_directories(path_to_external_library_source_code)
find_library(LIBNAME LibName_1 LibName_2 HINTS built_lib_directory)    

target_link_libraries(project_name ${LIBNAME})
install(TARGETS project_name DESTINATION installation_path)
当我构建项目时(使用
make
),它工作得很好,并且正确地创建了链接(我已经用
ldd
命令进行了检查)。但是,当我尝试安装它时(使用
makeinstall
),安装路径中生成的文件不会链接到指定的共享库

再次使用
ldd
,我检查了在安装路径中生成的库没有找到共享库,尽管它在构建路径中生成的库中找到。我怎样才能解决这个问题

谢谢


Pd:我在
Ubuntu 16.04.2 LTS
中使用
CMake 3.5.1
,我发现这个问题可以通过使用命令
find_library
来解决,指定构建的共享库的路径,执行如下操作:

cmake_minimum_required(VERSION 2.8.12)
project(project_name)

include_directories(path_to_external_library_source_code)
add_subdirectory(path_to_external_library_header_files subproject/external_library)

target_link_libraries(project_name external_library)
install(TARGETS project_name DESTINATION installation_path)
cmake_minimum_required(VERSION 2.8.12)
project(project_name)

include_directories(path_to_external_library_source_code)
find_library(LIBNAME LibName_1 LibName_2 HINTS built_lib_directory)    

target_link_libraries(project_name ${LIBNAME})
install(TARGETS project_name DESTINATION installation_path)

在某些路径中查找共享库,这些路径在
/etc/ld.so.conf
中配置

如果您的共享库位于这些路径之一,则应该找到它。如果不在这些路径中,则有四个选项:

  • 将库安装在系统默认路径中(这对于实验软件可能不可取,甚至在您许可的情况下也可能不可取)
  • 编辑系统范围内的搜索路径(实际上不希望触摸系统范围内的设置,并且可能由于权限原因而不可能)
  • 设置
    LD_LIBRARY_PATH
    (不推荐,因为它会覆盖系统搜索路径,基本上是一种调试功能),或者
  • 设置RPATH,即“告诉”/“硬编码”二进制文件在何处查找其库
  • 发生的情况是,CMake自动设置二进制文件的RPATH,以引用项目构建中的共享库
    ${CMake_binary_DIR}
    。(您希望根据刚刚构建的库(而不是昨天安装的库)测试二进制文件。)

    默认情况下,
    make install
    会清除此RPATH设置(因此,
    ${CMAKE\u BINARY\u DIR}
    中的库不再被引用,而是在系统搜索路径中搜索)

    假设您正在安装到不在系统搜索路径中的目标,您的二进制文件现在不再“看到”其共享库。咨询
    让CMake将二进制文件的RPATH设置为共享库的安装路径。

    您认为
    添加子目录(路径到外部库头文件子项目/外部库)有什么作用?在
    指向外部库头文件的路径中是否有CMakeLists.txt文件
    ?为什么要将外部库作为项目的一部分?是的,
    path\u to\u external\u library\u header\u files
    中有一个
    CMakeLists.txt
    ,它将共享库的
    .cpp
    .h
    文件添加到项目中。我希望共享库成为创建库的一部分,这就是为什么我使用
    add_子目录
    (我希望我做得对)。