问题描述
我在OS X 10.10.5(Yosemite)上使用CLion,并尝试使用日志记录增强(1.58.0)库.
I am using CLion on OS X 10.10.5 (Yosemite) and trying to use the logging boost (1.58.0) library.
我使用brew install boost
安装了boost.我检查了Boost库是64位的,也是我的可执行文件.
I installed boost using brew install boost
. I checked that the boost libraries are 64-bit and so is my executable.
我的CMakeLists.txt
看起来像这样:
set(CMAKE_VERBOSE_MAKEFILE on)
cmake_minimum_required(VERSION 3.3)
project(gmch)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(Boost_USE_MULTITHREADED ON)
find_package(Boost 1.58.0 COMPONENTS log REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
set(SOURCE_FILES main.cpp util/timing.hpp)
add_executable(gmch ${SOURCE_FILES})
target_link_libraries(gmch ${Boost_LIBRARIES})
我的main.cpp
看起来像这样:
#include <iostream>
#include <boost/log/trivial.hpp>
int main() {
BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
std::cout << "Hello, World!" << std::endl;
return 0;
}
构建时会得到以下输出:
I get the following output when building:
/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/iulian/Library/Caches/clion11/cmake/generated/e5dc5944/e5dc5944/Debug --target gmch -- -j 4
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -H/Users/iulian/ClionProjects/gmch -B/Users/iulian/Library/Caches/clion11/cmake/generated/e5dc5944/e5dc5944/Debug --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 gmch
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -H/Users/iulian/ClionProjects/gmch -B/Users/iulian/Library/Caches/clion11/cmake/generated/e5dc5944/e5dc5944/Debug --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_progress_start /Users/iulian/Library/Caches/clion11/cmake/generated/e5dc5944/e5dc5944/Debug/CMakeFiles 2
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 CMakeFiles/gmch.dir/all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/gmch.dir/build.make CMakeFiles/gmch.dir/depend
cd /Users/iulian/Library/Caches/clion11/cmake/generated/e5dc5944/e5dc5944/Debug && /Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_depends "Unix Makefiles" /Users/iulian/ClionProjects/gmch /Users/iulian/ClionProjects/gmch /Users/iulian/Library/Caches/clion11/cmake/generated/e5dc5944/e5dc5944/Debug /Users/iulian/Library/Caches/clion11/cmake/generated/e5dc5944/e5dc5944/Debug /Users/iulian/Library/Caches/clion11/cmake/generated/e5dc5944/e5dc5944/Debug/CMakeFiles/gmch.dir/DependInfo.cmake --color=
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/gmch.dir/build.make CMakeFiles/gmch.dir/build
[ 50%] Linking CXX executable gmch
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_link_script CMakeFiles/gmch.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -std=c++11 -g -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/gmch.dir/main.cpp.o -o gmch
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() 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(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::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[3]: *** [gmch] Error 1
make[2]: *** [CMakeFiles/gmch.dir/all] Error 2
make[1]: *** [CMakeFiles/gmch.dir/rule] Error 2
make: *** [gmch] Error 2
如何链接到Boost库?
How can I link to the boost libraries?
这是CMake的BOOST变量值:
Here are the BOOST variable values from CMake:
Boost Include: /usr/local/include
Boost Libraries: /usr/local/lib
Boost Libraries: /usr/local/lib/libboost_log-mt.dylib
因此,它实际上找到了库...
Therefore it actually finds the libraries...
如果我执行$file /usr/local/lib/libboost_log-mt.dylib
,我会得到
/usr/local/lib/libboost_log-mt.dylib: Mach-O 64-bit dynamically linked shared library x86_64
所以这个库是x86_64库,这是正确的,因为我还将我的应用程序构建为64位应用程序.
So this library is an x86_64 library, which is correct because I am also building my app as a 64 bit app.
我检查了boost是针对libc ++库而不是针对libstdc ++构建的.使用自制软件安装时,这似乎是默认设置.因此,这应该不是问题.我注意到在较旧版本的OS X上对此问题进行了一些较早的讨论,但这似乎不再是一个问题.
I checked that boost is built against the libc++ library, and not libstdc++. This seems to be the default now when installing with homebrew. So this should not be a problem. I noticed some older discussions regarding this issue on older versions of OS X, but this does not seem to be an issue anymore.
推荐答案
您必须设置#define BOOST_LOG_DYN_LINK 1
,这可以通过使用add_definitions(-DBOOST_LOG_DYN_LINK=1)
的cmake来完成.
You have to set #define BOOST_LOG_DYN_LINK 1
, this can be done with cmake using add_definitions(-DBOOST_LOG_DYN_LINK=1)
.
这篇关于将Boost与CMake链接时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!