我使用boost 1.60并在编译项目时注意到一个奇怪的事情。我静态编译了boost并将其用于我的静态slib
项目。
静态库使用boost :: log进行全部调试。
然后我将这个slib
包含并链接到一个可执行文件中,并得到了很多这样的错误:
[ 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*)'
很奇怪,因为我确定包含boost_filesystem
。
只要我将boost_filesystem
添加到我的可执行文件
if (boost::filesystem::exists("ttt.txt"))
std::cout << "tt";
所有错误都消失了,编译和链接也很顺利。
gcc是否有可能从我的boost_filesystem
静态库中优化slib
?
编辑:我发布了静态库的CMake文件
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})
以及链接它的简单可执行文件(几乎是一个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!
使用conan静态编译boost库(Boost:shared = False)。