如何正确链接Boost共享库

时间:2019-11-29 12:13:05

标签: c++ boost cmake dynamic-linking

我正在使用Ubuntu 16.04,cmake 3.12.2,g ++ 8.3.0。我有一个使用Boost Filesystem组件的非常简单的程序;

#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <iostream>

int main(int, char** argv)
{
    namespace fs = boost::filesystem;
    fs::path absolute_arg0 = fs::absolute(argv[0]);
    std::cout << "Hello, " << absolute_arg0 << std::endl;
}

然后用cmake来构建它

cmake_minimum_required(VERSION 3.10)
project(demo VERSION 1.0.0 LANGUAGES CXX)

# Set default build type, if none is given
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Debug" CACHE STRING
        "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel."
        FORCE)
endif()

# Set default language settings
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_EXTENSIONS OFF)

# Set default compiler options
if(CMAKE_COMPILER_IS_GNUCXX)
    list(APPEND common_flags -pedantic -Wall -Wextra -Wdeprecated -Werror)
    list(APPEND common_flags -Wshadow -Wunused -Wuninitialized)
    list(APPEND common_flags -Wpointer-arith -Winit-self -Wfloat-equal)
    list(APPEND common_flags -fno-strict-aliasing)
    list(APPEND cxx_only_flags -Wsign-promo -Wnon-virtual-dtor -Wctor-dtor-privacy)
    list(APPEND cxx_only_flags -Woverloaded-virtual -Wold-style-cast)

    add_compile_options(${common_flags})
    add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:${cxx_only_flags}>")
endif()

find_package(Boost REQUIRED COMPONENTS filesystem system)

add_executable(${CMAKE_PROJECT_NAME} demo.cpp)
target_link_libraries(${CMAKE_PROJECT_NAME}
    PRIVATE
    ${Boost_LIBRARIES}
)
target_include_directories(${CMAKE_PROJECT_NAME}
    PRIVATE
    ${Boost_INCLUDE_DIRS}
)
message(" Boost_LIBRARIES: ${Boost_LIBRARIES} ")
message(" Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS} ")

install(TARGETS ${CMAKE_PROJECT_NAME}
    RUNTIME DESTINATION ${APD_INSTALL_ROOT}/opt/${CMAKE_PROJECT_NAME}/bin)

使用

Boost_LIBRARIES=/path/to/lib/libboost_filesystem-mt.so;/path/to/lib/libboost_system-mt.so 

但是当我运行可执行文件时,它给了我

  加载共享库时出现

错误:libboost_filesystem.so.1.68.0:   无法打开共享对象文件:没有这样的文件或目录

链接器链接到libboost_filesystem.so.1.68.0,而不链接到libboost_filesystem-mt.so。为什么会这样?

0 个答案:

没有答案