我正在尝试在macOS Sierra中使用我的c ++项目中的boost。我已经使用自制啤酒安装了增强功能,如下所示:
brew install boost
我的CMakeLists.txt文件看起来像 cmake_minimum_required(版本3.8) 项目(SR)
set(CMAKE_CXX_STANDARD 14)
set(Boost_USE_STATIC_LIBS OFF) # only find static libs
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.64.0 COMPONENTS system log log_setup thread date_time filesystem regex chrono atomic REQUIRED)
include_directories(src)
include_directories(lib/easylogging++)
include_directories(${Boost_INCLUDE_DIRS})
add_subdirectory(src)
add_subdirectory(sr_tests)
set(SOURCE_FILES main.cpp lib/easylogging++/easylogging++.cc)
add_executable(sr_out ${SOURCE_FILES})
target_link_libraries(sr_out ${Boost_LIBRARIES} sr)
我的main.cpp很简单 #include
int main() {
BOOST_LOG_TRIVIAL(info) << "hi";
return 1;
}
在构建阶段,二进制文件无法与
链接➜ build git:(master) ✗ cmake ..
-- The C compiler identification is AppleClang 8.1.0.8020042
-- The CXX compiler identification is AppleClang 8.1.0.8020042
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - found
-- Found Threads: TRUE
-- Boost version: 1.64.0
-- Found the following Boost libraries:
-- system
-- log
-- log_setup
-- thread
-- date_time
-- filesystem
-- regex
-- chrono
-- atomic
-- Configuring done
-- Generating done
-- Build files have been written to: /Volumes/sr/sr/build
➜ build git:(master) ✗ make VERBOSE=1
/usr/local/Cellar/cmake/3.8.2/bin/cmake -H/Volumes/sr/sr -B/Volumes/sr/sr/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/Cellar/cmake/3.8.2/bin/cmake -E cmake_progress_start /Volumes/sr/sr/build/CMakeFiles /Volumes/sr/sr/build/CMakeFiles/progress.marks
/Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/Makefile2 all
/Library/Developer/CommandLineTools/usr/bin/make -f src/CMakeFiles/sr.dir/build.make src/CMakeFiles/sr.dir/depend
cd /Volumes/sr/sr/build && /usr/local/Cellar/cmake/3.8.2/bin/cmake -E cmake_depends "Unix Makefiles" /Volumes/sr/sr /Volumes/sr/sr/src /Volumes/sr/sr/build /Volumes/sr/sr/build/src /Volumes/sr/sr/build/src/CMakeFiles/sr.dir/DependInfo.cmake --color=
/Library/Developer/CommandLineTools/usr/bin/make -f src/CMakeFiles/sr.dir/build.make src/CMakeFiles/sr.dir/build
make[2]: Nothing to be done for `src/CMakeFiles/sr.dir/build'.
[ 30%] Built target sr
/Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/sr_out.dir/build.make CMakeFiles/sr_out.dir/depend
cd /Volumes/sr/sr/build && /usr/local/Cellar/cmake/3.8.2/bin/cmake -E cmake_depends "Unix Makefiles" /Volumes/sr/sr /Volumes/sr/sr /Volumes/sr/sr/build /Volumes/sr/sr/build /Volumes/sr/sr/build/CMakeFiles/sr_out.dir/DependInfo.cmake --color=
/Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/sr_out.dir/build.make CMakeFiles/sr_out.dir/build
[ 40%] Linking CXX executable sr_out
/usr/local/Cellar/cmake/3.8.2/bin/cmake -E cmake_link_script CMakeFiles/sr_out.dir/link.txt --verbose=1
/Library/Developer/CommandLineTools/usr/bin/c++ -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/sr_out.dir/main.cpp.o CMakeFiles/sr_out.dir/lib/easylogging++/easylogging++.cc.o -o sr_out /usr/local/lib/libboost_system-mt.dylib /usr/local/lib/libboost_log-mt.dylib /usr/local/lib/libboost_log_setup-mt.dylib /usr/local/lib/libboost_thread-mt.dylib /usr/local/lib/libboost_date_time-mt.dylib /usr/local/lib/libboost_filesystem-mt.dylib /usr/local/lib/libboost_regex-mt.dylib /usr/local/lib/libboost_chrono-mt.dylib /usr/local/lib/libboost_atomic-mt.dylib src/libsr.a
Undefined symbols for architecture x86_64:
"boost::log::v2s_mt_posix::record_view::public_data::destroy(boost::log::v2s_mt_posix::record_view::public_data const*)", referenced from:
boost::log::v2s_mt_posix::record::reset() in main.cpp.o
"boost::log::v2s_mt_posix::aux::stream_provider<char>::release_compound(boost::log::v2s_mt_posix::aux::stream_provider<char>::stream_compound*)", referenced from:
boost::log::v2s_mt_posix::aux::record_pump<boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level> >::auto_release::~auto_release() in main.cpp.o
"boost::log::v2s_mt_posix::aux::stream_provider<char>::allocate_compound(boost::log::v2s_mt_posix::record&)", referenced from:
boost::log::v2s_mt_posix::aux::record_pump<boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level> >::record_pump(boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level>&, boost::log::v2s_mt_posix::record&) in main.cpp.o
"boost::log::v2s_mt_posix::aux::unhandled_exception_count()", referenced from:
boost::log::v2s_mt_posix::aux::record_pump<boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level> >::record_pump(boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level>&, boost::log::v2s_mt_posix::record&) in main.cpp.o
boost::log::v2s_mt_posix::aux::record_pump<boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level> >::~record_pump() in main.cpp.o
"boost::log::v2s_mt_posix::core::open_record(boost::log::v2s_mt_posix::attribute_set const&)", referenced from:
boost::log::v2s_mt_posix::record boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level>, boost::log::v2s_mt_posix::sources::multi_thread_model<boost::log::v2s_mt_posix::aux::light_rw_mutex> >::open_record_unlocked<boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::severity, boost::log::v2s_mt_posix::trivial::severity_level const> >(boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::severity, boost::log::v2s_mt_posix::trivial::severity_level const> const&) in main.cpp.o
"boost::log::v2s_mt_posix::core::push_record_move(boost::log::v2s_mt_posix::record&)", referenced from:
boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level>, boost::log::v2s_mt_posix::sources::multi_thread_model<boost::log::v2s_mt_posix::aux::light_rw_mutex> >::push_record_unlocked(boost::log::v2s_mt_posix::record&&) in main.cpp.o
"boost::log::v2s_mt_posix::sources::aux::get_severity_level()", referenced from:
boost::log::v2s_mt_posix::sources::aux::severity_level<boost::log::v2s_mt_posix::trivial::severity_level>::set_value(boost::log::v2s_mt_posix::trivial::severity_level) in main.cpp.o
"boost::log::v2s_mt_posix::trivial::logger::get()", referenced from:
_main in main.cpp.o
"boost::log::v2s_mt_posix::core::get_logging_enabled() const", referenced from:
boost::log::v2s_mt_posix::record boost::log::v2s_mt_posix::sources::basic_composite_logger<char, boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level>, boost::log::v2s_mt_posix::sources::multi_thread_model<boost::log::v2s_mt_posix::aux::light_rw_mutex>, boost::log::v2s_mt_posix::sources::features<boost::log::v2s_mt_posix::sources::severity<boost::log::v2s_mt_posix::trivial::severity_level> > >::open_record<boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::severity, boost::log::v2s_mt_posix::trivial::severity_level const> >(boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::severity, boost::log::v2s_mt_posix::trivial::severity_level const> const&) in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [sr_out] Error 1
make[1]: *** [CMakeFiles/sr_out.dir/all] Error 2
make: *** [all] Error 2
➜ build git:(master) ✗
我真的很感激帮助你做好准备。
答案 0 :(得分:2)
问题是Boost Logging库必须动态链接。我修复了我的CMakeLists.txt文件以添加以下行:
add_definitions(-DBOOST_LOG_DYN_LINK=1)
这解决了问题