将 Boost 与 CMake 链接时出错
Error linking Boost with CMake
我在 OS X 10.10.5 (Yosemite) 上使用 CLion,并尝试使用 logging boost (1.58.0) 库。
我使用 brew install boost
安装了 boost。我检查了 boost 库是 64 位的,我的可执行文件也是 64 位的。
我的 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;
}
构建时我得到以下输出:
/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
如何 link 到 boost 库?
编辑 1:
这是来自 CMake 的 BOOST 变量值:
Boost Include: /usr/local/include
Boost Libraries: /usr/local/lib
Boost Libraries: /usr/local/lib/libboost_log-mt.dylib
因此它实际上找到了库...
编辑 2:
如果我这样做 $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 位应用程序。
编辑 3:
我检查过 boost 是针对 libc++ 库而不是 libstdc++ 构建的。现在这似乎是使用自制软件安装时的默认设置。所以这应该不是问题。我注意到一些关于旧版本 OS X 上这个问题的旧讨论,但这似乎不再是一个问题。
阅读输出让我的怀疑更加清晰,link 没有 任何 Boost 库,因为你没有向你的 find_package
命令。
您需要在 find_package
命令中明确列出要查找和使用的库。
您必须设置 #define BOOST_LOG_DYN_LINK 1
,这可以通过使用 add_definitions(-DBOOST_LOG_DYN_LINK=1)
的 cmake 来完成。
我在 OS X 10.10.5 (Yosemite) 上使用 CLion,并尝试使用 logging boost (1.58.0) 库。
我使用 brew install boost
安装了 boost。我检查了 boost 库是 64 位的,我的可执行文件也是 64 位的。
我的 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;
}
构建时我得到以下输出:
/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
如何 link 到 boost 库?
编辑 1:
这是来自 CMake 的 BOOST 变量值:
Boost Include: /usr/local/include
Boost Libraries: /usr/local/lib
Boost Libraries: /usr/local/lib/libboost_log-mt.dylib
因此它实际上找到了库...
编辑 2:
如果我这样做 $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 位应用程序。
编辑 3:
我检查过 boost 是针对 libc++ 库而不是 libstdc++ 构建的。现在这似乎是使用自制软件安装时的默认设置。所以这应该不是问题。我注意到一些关于旧版本 OS X 上这个问题的旧讨论,但这似乎不再是一个问题。
阅读输出让我的怀疑更加清晰,link 没有 任何 Boost 库,因为你没有向你的 find_package
命令。
您需要在 find_package
命令中明确列出要查找和使用的库。
您必须设置 #define BOOST_LOG_DYN_LINK 1
,这可以通过使用 add_definitions(-DBOOST_LOG_DYN_LINK=1)
的 cmake 来完成。