I am developing a library (let's say mylibrary) to be used by some third-party applications. My library is build and compiled by cmake (on CentOS 7 and g++ 4.8) and uses some boost libraries, such as thread, log, system, filesystem and date_time (version 1.61.0). To test my lib, I've developed a very simple tester (let's call it tester). The point is that my library can by successfully built and linked, but the tester cannot. Let me show the contents of cmake files below:
In the main cmake file that is dedicated to building mylibrary, I have added boost in this way:
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
SET(Boost_ADDITIONAL_VERSIONS "1.51.0" "1.52.0"
"1.53.0" "1.54.0" "1.55.0" "1.56.0" "1.57.0" "1.58.0"
"1.60.0" "1.61.0" "1.61.0" "1.62.0" "1.63.0" "1.64.0")
find_package(Threads)
find_package(Boost REQUIRED)
find_package(Boost 1.61.0 COMPONENTS thread log log_setup filesystem system date_time)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
To add the library, I have used these lines of code:
set(MYLIBRARY_SRC
${MYLIBRARY_SOURCE_DIR}/src/folder1/File1.cpp
${MYLIBRARY_SOURCE_DIR}/src/folder2/File2.cpp
${MYLIBRARY_SOURCE_DIR}/src/folder3/File3.cpp)
add_library (mylibrary ${MYLIBRARY_SRC})
In the last lines of this cmake files, I have added the tester directory in this way:
if(ENABLE_TESTER)
message(STATUS "tester is enabled and will be built")
add_subdirectory (tester)
endif(ENABLE_TESTER)
and for the tester, I have a very simple cmake file like this:
add_executable(tester tester.cpp)
message(STATUS , "Boost_libraries are ${Boost_LIBRARIES}")
target_link_libraries(tester mylibrary ${Boost_LIBRARIES})
Now, whenever I build my project, my library is built successfully, but when the tester is going to be linked, I face lots of linker errors like this:
../../lib/mylibrary.a (Logger.cpp.o): In function `void boost::log::v2s_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> >::aligned_write<wchar_t>(wchar_t const*, long)':
/home/john/local/include/boost/log/utility/formatting_ostream.hpp:702: undefined reference to `boost::log::v2s_mt_posix::aux::code_convert(wchar_t const*, unsigned long, std::string&, std::locale const&)'
/home/john/local/include/boost/log/utility/formatting_ostream.hpp:696: undefined reference to `boost::log::v2s_mt_posix::aux::code_convert(wchar_t const*, unsigned long, std::string&, std::locale const&)'
The point is that I have printed the Boost_LIBRARIES variable before linking the tester and everything seems fine.