为什么这个提升示例没有链接到OS X上
Why does this boost example not link on OS X?
我有一个非常小的示例应用程序,它可以在Ubuntu上正确构建、链接和运行。但当我在OS X上尝试时,我会遇到无法解决的链接错误。
这是我的CMakeLists.txt文件:
PROJECT ( Test C CXX )
CMAKE_MINIMUM_REQUIRED ( VERSION 2.8 )
SET ( Boost_USE_STATIC_LIBS ON )
SET ( Boost_USE_MULTITHREADED ON )
SET ( Boost_USE_STATIC_RUNTIME ON )
FIND_PACKAGE ( Boost REQUIRED COMPONENTS regex system date_time )
FIND_PACKAGE ( Threads REQUIRED )
INCLUDE_DIRECTORIES ( AFTER ${Boost_INCLUDE_DIR} )
SET ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++" )
ADD_EXECUTABLE ( myapp test.cpp )
TARGET_LINK_LIBRARIES ( myapp ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} )
这是我的test.cpp文件:
#include <boost/regex.hpp>
#include <iostream>
#include <chrono>
int main()
{
boost::regex e( "he[^ ]{3} " );
if (boost::regex_search( "hello world", e ))
{
std::cout << "found!" << std::endl;
}
return 0;
}
运行cmake ..
运行良好,它发现Boost版本1.57.0和AppleClang 5.1.05030040。但当我尝试运行make
时,我会得到大约250行错误,从开始
Linking CXX executable myapp
Undefined symbols for architecture x86_64:
"boost::re_detail::perl_matcher<char const*, std::__1::allocator<boost::sub_match<char const*> >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::construct_init(boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)", referenced from:
boost::re_detail::perl_matcher<char const*, std::__1::allocator<boost::sub_match<char const*> >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::perl_matcher(char const*, char const*, boost::match_results<char const*, std::__1::allocator<boost::sub_match<char const*> > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags, char const*) in test.cpp.o
"boost::re_detail::perl_matcher<char const*, std::__1::allocator<boost::sub_match<char const*> >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::find()", referenced from:
bool boost::regex_search<char const*, char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(char const*, char const*, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags) in test.cpp.o
"std::string::compare(char const*) const", referenced from:
unsigned int boost::re_detail::find_sort_syntax<boost::re_detail::cpp_regex_traits_implementation<char>, char>(boost::re_detail::cpp_regex_traits_implementation<char> const*, char*) in libboost_regex.a(instances.o)
我在操作系统X 10.8.5〔Mountain Lion〕上使用:
$ clang++ --version
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix
编辑:按照要求,这是带有VERBOSE=1
:的命令
[100%] Building CXX object CMakeFiles/myapp.dir/test.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -std=c++11 -stdlib=libc++ -I/usr/local/include -o CMakeFiles/myapp.dir/test.cpp.o -c /Users/Administrator/tmp/test.cpp
Linking CXX executable myapp
/usr/local/Cellar/cmake/3.2.1/bin/cmake -E cmake_link_script CMakeFiles/myapp.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -std=c++11 -stdlib=libc++ -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/myapp.dir/test.cpp.o -o myapp /usr/local/lib/libboost_regex.a /usr/local/lib/libboost_system.a /usr/local/lib/libboost_date_time.a
Undefined symbols for architecture x86_64:
"boost::re_detail::perl_matcher<char const*, std::__1::allocator<boost::sub_match<char const*> >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::construct_init(boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)", referenced from:
我怀疑您的Boost库是使用libstdc++编译的,并且您正在使用libc++进行构建。
编辑:
要使Boost使用libc++而不是libstdc++,请使用以下命令安装:
brew install boost --c++11
要查看boost是否链接到libc++或libstdc++,请尝试以下操作:
otool -L /usr/local/lib/libboost_regex-mt.dylib
相关文章:
- 无法在 Mac OS X 上链接静态资源库
- 在Mac OS X Mojave上使用AppleClang编译和链接OpenMP
- 标准::字符串::空的未定义符号错误;Mac OS High Sierra 上的 c++ 标准方法链接错误
- Windows OS上的静态链接
- CMAKE OS X CLION.如何链接自定义动态库
- mbed OS 5 项目中链接了哪些文件
- cppunit在Mac OS X上与Clang链接错误
- 在OS X上使用clang编译boost regex时发生链接错误
- 在 Mac OS X 上编译针对 lib lua 链接的动态库会导致"lua: multiple Lua VMs detected"
- 在 OS X 上编译lsd_slam会引发链接错误 ld:找不到 -lGL 的库
- OS X Mavericks 上的 gtest 的链接器错误
- OS X 上的链接器 (ld):如何使用 -wl,--start-group(和 --end-group)
- CGAL:如何成功编译和链接CGAL示例(在Mac OS X 10.9 Mavericks上)
- 升级到Mac OS X 10.9 / Xcode 5.0.1后C++链接错误
- 在 Mac OS X 上链接不同的C++标准库
- CppUnit OS X 10.10的CMake链接器错误
- POCO链接错误,OS X 10.11.2上体系结构x86_64的未定义符号
- Mac OS X 10.9.5上的HealPix C++链接错误
- 'ld: unknown option: -rpath=./libs' - 在 Mac OS 10.7.5 上构建 OpenFramework 插件时出现链接错误
- 编译过程中无法从加速框架中链接BLAS -OS Yosemite 10.0.5