为什么这个提升示例没有链接到OS X上

Why does this boost example not link on OS X?

本文关键字:链接 OS 为什么      更新时间:2023-10-16

我有一个非常小的示例应用程序,它可以在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