-stdlib = libstdc ++的std :: future和clang [英] std::future and clang with -stdlib=libstdc++

查看:151
本文介绍了-stdlib = libstdc ++的std :: future和clang的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The following program fails to link with clang and -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 ++进行构建时,发生以下链接错误:

When building with clang and -stdlib=libstdc++, the following linking error occurs:

$ 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 的简单程序可以很好地构建,例如:

However, a simple program w/o future builds just fine, e.g.:

$ 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.

System is OSX 10.10.4 with macports.

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

I can't figure out what is the problem. Thanks!

推荐答案

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

This is an incompatibility between GCC and Clang on Mac OS X.

GCC发出对 ___ emutls_v._ZSt15__once_callable 的引用,而clang发出对 __ ZSt15__once_callable 的引用.

GCC emits a reference to ___emutls_v._ZSt15__once_callable while clang emits a reference to __ZSt15__once_callable.

不幸的是, __ ZSt15__once_callable ___ emutls_v._ZSt15__once_callable 不兼容,因此请执行以下操作:

Unfortunately, __ZSt15__once_callable and ___emutls_v._ZSt15__once_callable are not compatible, so doing something like:

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实现.

I also came accross this LLVM bug report: http://lists.cs.uiuc.edu/pipermail/llvmbugs/2014-August/035744.html which probably means clang will never add for support GCC's emutls implementation.

似乎几个小时前,在

It looks like support for emutls was added to clang trunk a couple of hours ago in r243438 via -femulated-tls.

这篇关于-stdlib = libstdc ++的std :: future和clang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆