Can't Link Program Using Boost.Filesystem

Can't link program using Boost.Filesystem

Boost filesystem is one of the Boost library that have some ABI problem relative to function signature change due to C++0x or C++11. cf Boost ticket : https://svn.boost.org/trac/boost/ticket/6779

You have three solutions:

  1. Inhibit C++11 scoped enums in concerned Boost header files included in your programs with #include (cf http://www.ridgesolutions.ie/index.php/2013/05/30/boost-link-error-undefined-reference-to-boostfilesystemdetailcopy_file/):

    #define BOOST_NO_CXX11_SCOPED_ENUMS
    #include <boost/filesystem.hpp>
    #undef BOOST_NO_CXX11_SCOPED_ENUMS

    But this solution is not a complete one and I read it does not work for everybody.

  2. Build BOOST with the C++11 option (the same options you use for your application): http://hnrkptrsn.github.io/2013/02/26/c11-and-boost-setup-guide

    I read also it does not work for everybody.

  3. Set up a cross compiler dedicated to your application where you rebuild all the libraries you need in a dedicated environment. That ensures coherence plus stability plus more maintainability, and is certainly the solution to recommend.
    I have not read if it has been tested - probably yes, and probably it works. Anyway, cross compiling is well mastered now in computer science. You will find many good tutorials and support for it. In Linux Gentoo, they have the marvelous sys-devel/crossdev package that makes it very easy.

In my own case, solution 1 has solved the problem. As soon as I encounter another one, I will switch to solution 3. So, I have not yet tested it.

Cannot link when using boost::filesystem

There are many options to the linker (ld) to specify the search path to resolve shared libraries, man ld will give you all the options. Suppose you have boost installed in /usr/local/lib, you could add one of these options to gcc to pass along to the linker:

  • -L=/usr/local/lib

    Directories specified on the command line are searched before the default directories. All -L options apply to all -l options, regardless of the order in which the options appear.
    If searchdir begins with "=", then the "=" will be replaced by the sysroot prefix, a path specified when the linker is configured. The -L option only sets a compile-time library search path; if you want a shared library to be found at runtime then its directory must be known at runtime.

  • -Wl,-rpath,/usr/local/lib

    Add a directory to the runtime library search path. This is used when linking an ELF executable with shared objects. All -rpath arguments are concatenated and passed to the runtime linker, which uses them to locate shared objects at runtime. The -rpath option is also used when locating shared objects which are needed by shared objects explicitly included in the link; see the description of the -rpath-link option. If -rpath is not used when linking an ELF executable, the contents of the environment variable "LD_RUN_PATH" will be used if it is defined.

Another alternative is to add to your LD_LIBRARY_PATH the location of your boost libraries.

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

The linker (ld) uses LD_LIBRARY_PATH as one of the search paths to locate required shared libraries.

You can read more about the linker and shared libraries here.

To fully understand why your installation isn't finding the boost libraries by default you might find this answer at stackexchange informative.

This SO answer suggests using boost m4.

Boost::FileSystem Linking Problem

Rebuild the Boost library with address-model=64 b2 command line switch. This builds 64 bit libraries.

C++ BOOST undefined reference to `boost::filesystem::detail::copy_file

There is a workaround for this problem, replace

#include <boost/filesystem.hpp>

with

#define BOOST_NO_CXX11_SCOPED_ENUMS
#include <boost/filesystem.hpp>
#undef BOOST_NO_CXX11_SCOPED_ENUMS

Or, preferably, add -DBOOST_NO_CXX11_SCOPED_ENUMS to your compiler flags

Can't link Boost 1.63.0 through CMake

Replace

find_package(BOOST 1.6.0 REQUIRED)

by

find_package(Boost 1.63.0 REQUIRED filesystem system)

otherwise CMake doesn't know which boost library to link against.

Error linking to Boost filesystem using cmake on cygwin

You need to tell the linker to link Boost.Filesystem and Boost.System libraries.

You can do:

target_link_libraries(RayTracer
${Boost_PROGRAM_OPTIONS_LIBRARIES}
${Boost_FILESYSTEM_LIBRARIES}
${Boost_SYSTEM_LIBRARIES}
)

or if you just want to link all the libs specified in your find_package(Boost...) call, you can do:

target_link_libraries(RayTracer ${Boost_LIBRARIES})

For further details on the FindBoost CMake module, see the docs or run:

cmake --help-module FindBoost

Linking with error using boost library and clion

Change this line in your CMakeList.txt:

find_package(Boost)

by

find_package(Boost COMPONENTS system filesystem REQUIRED)

And don't forget to link with your target:

target_link_libraries(untitled 
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
)

So your CMakeList.txt will be:

cmake_minimum_required(VERSION 3.7)
project(untitled)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp)
add_executable(untitled ${SOURCE_FILES})

find_package(Boost COMPONENTS system filesystem REQUIRED)
IF (Boost_FOUND)
include_directories(${Boost_INCLUDE_DIR})
target_link_libraries(untitled
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
)
endif()


Related Topics



Leave a reply



Submit