使用 CMake 对 boost::iostreams::mapped_file_source::init() 的未定义引用

undefined reference to boost::iostreams::mapped_file_source::init() using CMake

使用 Boost Iostream 的最小示例的链接错误。看起来我还没有 linked with libboost_iostream,但是 CMake 报告说找到了库,其他使用 Boost 编译和 link 的应用程序没有任何问题。

使用 Cmake 构建:

cmake_minimum_required(VERSION 3.0)
project(mmap_example CXX)
set(TARGET mmap_example)

set(BOOST_MIN_VERSION "1.61.0")
set(Boost_ADDITIONAL_VERSIONS "1.61.0" "1.61")
set(Boost_USE_STATIC_LIBS ON)
set(BOOST_ROOT ${MY_BOOST_DIR})

find_package(Boost ${BOOST_MIN_VERSION} COMPONENTS iostreams REQUIRED)

set(CMAKE_CXX_FLAGS "-std=c++11 -std=gnu++1y -pthread")
set(CMAKE_EXE_LINKER_FLAGS "-std=c++11 -std=gnu++1y -pthread")

file(GLOB SOURCES *.cpp)

include_directories(${Boost_INCLUDE_DIRS}) 

add_executable(${TARGET} ${SOURCES})

target_link_libraries(${TARGET} ${Boost_IOSTREAMS})

C++ 本身:

#include <boost/iostreams/device/mapped_file.hpp>

namespace boost_io = boost::iostreams;

int main(int argc, char** argv) {

    boost_io::mapped_file_source file(argv[1]);
    return 0;
}

GCC 输出:

Linking CXX executable mmap_example CMakeFiles/mmap_example.dir/mmap.cpp.o: In function boost::iostreams::mapped_file_source::mapped_file_source<char*>(char* const&, unsigned int, long long): mmap.cpp:(.text._ZN5boost9iostreams18mapped_file_sourceC2IPcEERKT_jx[_ZN5boost9iostreams18mapped_file_sourceC5IPcEERKT_jx]+0x43): undefined reference to boost::iostreams::mapped_file_source::init()

gcc (Debian 4.9.2-10) 4.9.2

CMake 3.0.2

提升 1.61

我不确定 ${Boost_IOSTREAMS} 是否是正确的变量,据我所知,它应该是 ${Boost_LIBRARIES}(至少我一直这么用)。

您可以使用

检查变量是否真的设置了
message(STATUS "Boost_IOSTREAMS: ${Boost_IOSTREAMS}")

在你的 cmake 文件中。

您也可以使用

make all VERBOSE=1

列出所有命令,检查链接器命令行上存在哪些库。