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

2024-03-12

以下程序无法与 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。

我不知道出了什么问题。谢谢!


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

GCC 发出对___emutls_v._ZSt15__once_callable而 clang 发出对__ZSt15__once_callable.

很遗憾,__ZSt15__once_callable and ___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 http://lists.cs.uiuc.edu/pipermail/llvmbugs/2014-August/035744.html这可能意味着 clang 永远不会添加对 GCC emutls 实现的支持。


Edit:看起来对 emutls 的支持已在几个小时前添加到 clang trunk 中r243438 https://github.com/llvm-mirror/llvm/commit/dc73dc09f135cb1d36fd18f1918395ddf68cec63 via -femulated-tls.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

std::future 和 clang 与 -stdlib=libstdc++ 的相关文章

随机推荐