0

I'm using boost 1.60 and noticed a weird thing while compiling my project.. I compiled boost statically and used it into my static slib project.

The static library uses boost::log for debugging all over.

Then I included and linked this slib into an executable and got lots of errors like this:

[ 76%] Linking CXX executable ../bin/test
/home/user/aeb509b8f8eb8ba82af2bc50d4649570ba08ff42/lib/libboost_log.a(text_file_backend.o): In function `boost::log::v2s_mt_posix::sinks::file:
:aux::make_collector(boost::filesystem::path const&, unsigned long, unsigned long)':
text_file_backend.cpp:(.text+0x1b73): undefined reference to `boost::filesystem::detail::current_path(boost::system::error_code*)'
text_file_backend.cpp:(.text+0x1bda): undefined reference to `boost::filesystem::absolute(boost::filesystem::path const&, boost::filesystem::path const&)'
text_file_backend.cpp:(.text+0x1c11): undefined reference to `boost::filesystem::detail::create_directories(boost::filesystem::path const&, boost::system::error_code*)'

weird since I'm sure boost_filesystem is included.

As soon as I added a useless line which used boost_filesystem into my executable

if (boost::filesystem::exists("ttt.txt")) 
   std::cout << "tt";

all the errors disappeared and compilation and linking went fine.

Is it possible that gcc is optimizing away boost_filesystem from my slib static library??

Edit: I'm posting the CMake files for the static library

cmake_minimum_required(VERSION 3.2)

set (CMAKE_CXX_STANDARD 14)

project(slib)

add_subdirectory(src)

add_library(slib ${SRCS})

# Conan setup
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

find_package(Threads) # needed

target_include_directories(test PUBLIC ${CMAKE_SOURCE_DIR}/include ${CMAKE_BINARY_DIR})
target_link_libraries(test ${CONAN_LIBS} ${CMAKE_THREAD_LIBS_INIT})

and for the simple executable which links against it (almost a hello world)

project(test)

set(SRCS test.cpp)

add_executable(test ${SRCS})
target_include_directories(test PRIVATE ${CMAKE_SOURCE_DIR}/include
                                        ${Boost_INCLUDE_DIRS})

target_link_libraries(test boost_filesystem ${CONAN_LIBS} slib) # Link it in!

The boost libraries are statically compiled with conan (Boost:shared=False).

TesterPen
  • 1
  • 1
  • 1
    A static library is really nothing more than an archive of object files, it has no concept of dependencies and you don't even use a linker to create a static library. If the static library depends on another library, you need to link with *both* libraries when you link your program. – Some programmer dude Nov 09 '16 at 09:27
  • @Someprogrammerdude I'm already linking against both `boost_filesystem` and `slib`, isn't that what you mean? – TesterPen Nov 09 '16 at 09:29
  • Can you please show the relevant parts of your `CMakeList.txt` file? For the `slib` library and the `test` executable? – Some programmer dude Nov 09 '16 at 09:30
  • @Someprogrammerdude Done, thanks for your help – TesterPen Nov 09 '16 at 09:33
  • 1
    Try changing the order of the libraries when linking your `test` program. If library A depended on library B, then A has to be before B when linking. So place `boost_filesystem` after `slib`. – Some programmer dude Nov 09 '16 at 09:36
  • Uhm it's of no use, same error. I don't understand.. CMake should have figured out that `slib` depends on `boost_filesystem` (I even use file operations all over).. and yet it cannot seem to do the right thing – TesterPen Nov 09 '16 at 09:43
  • Unless you tell it to, then CMake will not know that your library depends on another library. Maybe try to [add an explicit dependency](https://cmake.org/cmake/help/v3.2/command/add_dependencies.html) for `slib`? – Some programmer dude Nov 09 '16 at 09:46
  • @Someprogrammerdude I tried with `target_link_libraries (test boost_filesystem)` but that still hasn't worked (same errors). I have other small executables which link against `slib` without a flaw.. and yet this `test` executable which only parses command line arguments with boost::program_options and link against `slib` fails. If I add a line which does something with boost::filesystem it succeeds. Why is that? – TesterPen Nov 09 '16 at 10:48
  • @Someprogrammerdude update: I found out that boost_log NEEDS boost_filesystem, therefore as you said boost_filesystem must come AFTER it. That fixed the issue. Now I need to figure out where CMake got that wrong order of lib libraries to link.. – TesterPen Nov 09 '16 at 11:01

0 Answers0