std :: future和clang with -stdlib = libstdc ++

时间:2015-07-24 18:05:21

标签: c++11 clang future libstdc++

以下程序无法与clang和-stdlib = libstdc ++:

链接
$ cat future.cpp
#include <iostream>
#include <future>

int main()
{
  std::future<int> f1 = std::async([](){ return 42; });
  f1.wait();
  std::cout << "Magic number is: " << f1.get() << std::endl;
}
$ g++-mp-5 future.cpp -std=c++11 && ./a.out
Magic number is: 42
$ clang++-mp=3.5 future.cpp -std=c++11 && ./a.out
Magic number is: 42

使用clang和-stdlib = libstdc ++构建时,会发生以下链接错误:

$ clang++-mp-3.5  future.cpp -std=c++11 -stdlib=libstdc++ -I/opt/local/include/gcc5/c++ -I/opt/local/include/gcc5/c++/x86_64-apple-darwin14 -L/opt/local/lib/gcc5 -lstdc++ && ./a.out 
Undefined symbols for architecture x86_64:
  "std::__once_call", referenced from:
      void std::call_once<void (std::__future_base::_State_baseV2::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*>(std::once_flag&, void (std::__future_base::_State_baseV2::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*&&, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*&&, bool*&&) in future-b6480b.o
      void std::call_once<void (std::thread::*)(), std::reference_wrapper<std::thread> >(std::once_flag&, void (std::thread::*&&)(), std::reference_wrapper<std::thread>&&) in future-b6480b.o
  "std::__once_callable", referenced from:
      void std::call_once<void (std::__future_base::_State_baseV2::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*>(std::once_flag&, void (std::__future_base::_State_baseV2::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*&&, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*&&, bool*&&) in future-b6480b.o
      void std::__once_call_impl<std::_Bind_simple<std::_Mem_fn<void (std::__future_base::_State_baseV2::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*)> (std::__future_base::_State_baseV2*, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*)> >() in future-b6480b.o
      void std::call_once<void (std::thread::*)(), std::reference_wrapper<std::thread> >(std::once_flag&, void (std::thread::*&&)(), std::reference_wrapper<std::thread>&&) in future-b6480b.o
      void std::__once_call_impl<std::_Bind_simple<std::_Mem_fn<void (std::thread::*)()> (std::reference_wrapper<std::thread>)> >() in future-b6480b.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

但是,一个简单的程序没有future构建得很好,例如:

$ cat simple.cpp
#include <iostream>
#include <future>

int main()
{
  std::cout << "Magic number is: " << 42 << std::endl;
}
$ clang++-mp-3.5  simple.cpp -std=c++11 -stdlib=libstdc++ -I/opt/local/include/gcc5/c++ -I/opt/local/include/gcc5/c++/x86_64-apple-darwin14 -L/opt/local/lib/gcc5 -lstdc++ && ./a.out 
Magic number is: 42

系统是带有macports的OSX 10.10.4。

我无法弄清楚问题是什么。谢谢!

2 个答案:

答案 0 :(得分:2)

这是Mac OS X上GCC和Clang之间的不兼容。

GCC会发出对___emutls_v._ZSt15__once_callable的引用,而clang会发出对__ZSt15__once_callable的引用。

不幸的是,__ZSt15__once_callable___emutls_v._ZSt15__once_callable 是不兼容的,所以做了类似的事情:

asm("__ZSt15__once_callable: jmp ___emutls_v._ZSt15__once_callable");

也不会工作。

我也遇到了这个LLVM错误报告:http://lists.cs.uiuc.edu/pipermail/llvmbugs/2014-August/035744.html这可能意味着clang永远不会添加支持GCC的emutls实现。

修改:几小时前r243438通过-femulated-tls向clang trunk添加了对emutls的支持。

答案 1 :(得分:0)

OSX上的Clang使用旧版本的libstdc ++。我相信它的版本4.2不支持许多C ++ 11功能。如果你想在OSX上使用带有clang的C ++ 11,你唯一的选择就是libc ++。

您也可以使用自制软件中的gcc-4.x,其中包含较新版本的libstdc ++。不幸的是,clang不会轻易链接到此版本的标准库。