使用Boost Iostreams在最小示例上链接错误。看起来我还没有链接到libboost_iostream,但是CMake报告找到了带有Boost的库,并且其他应用程序也可以编译并链接,而没有任何问题。
使用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_exampleCMakeFiles/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}
是要使用的正确变量,AFAIK应该是${Boost_LIBRARIES}
(至少这是我一直使用的变量)。
您可以通过使用以下命令检查变量是否确实设置
message(STATUS "Boost_IOSTREAMS: ${Boost_IOSTREAMS}")
在您的cmake文件中。
您也可以使用
make all VERBOSE=1
列出所有命令,检查链接器命令行上存在哪些库。
关于c++ - 使用CMake对boost::iostreams::mapped_file_source::init()的 undefined reference ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41481794/