首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >科技促进发展:未来与-stdlib=libstdc++

科技促进发展:未来与-stdlib=libstdc++
EN

Stack Overflow用户
提问于 2015-07-24 18:05:21
回答 3查看 2.7K关注 0票数 5

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

代码语言:javascript
运行
复制
$ 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++构建时,会发生以下链接错误:

代码语言:javascript
运行
复制
$ 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)

但是,一个简单的程序w/o future构建得很好,例如:

代码语言:javascript
运行
复制
$ 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

系统为OSX10.10.4和宏端口。

我不知道是什么问题。谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-07-26 17:22:30

这是GCC和Clang在Mac上的不兼容。

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 bug报告:http://lists.cs.uiuc.edu/pipermail/llvmbugs/2014-August/035744.html,这可能意味着clang永远不会为支持GCC的emutls实现而添加。

编辑:看起来像几个小时前通过-femulated-tlsr243438中添加了对emutls的支持。

票数 3
EN

Stack Overflow用户

发布于 2015-07-24 21:37:09

OSX上的Clang使用较早版本的libstdc++。我相信它的4.2版不支持许多C++11特性。如果您想在OSX上使用带有clang的C++11,您唯一的选择是libc++。

你也可以使用gcc-4.x的自制,其中将包括一个更新版本的libstdc++。不幸的是,clang将不容易地链接到这个版本的标准库。

票数 0
EN

Stack Overflow用户

发布于 2022-01-05 22:56:42

在构建GCC的标准库时,未定义的符号std::__once_call将在这里解释,在使用编译器标志-femulated-tls时,应该对其进行修复。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31617034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档