Search code examples
c++cmakeboost-iostreams

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


Linking error on minimal example with Boost Iostreams. Looks like I have not linked with libboost_iostream, however CMake reports that library is found and other applications with Boost compile and link without any problems.

Using Cmake for build:

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++ itself:

#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 output:

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

Boost 1.61


Solution

  • I'm not sure that ${Boost_IOSTREAMS} is the correct variable to use, AFAIK it should be ${Boost_LIBRARIES} (at least that is what I always use).

    You can check if the variable is really set by using

    message(STATUS "Boost_IOSTREAMS: ${Boost_IOSTREAMS}")
    

    in your cmake file.

    You can also use

    make all VERBOSE=1
    

    to list all the commands, to check what libraries are present on the linker command line.