0

我正在通过 Eclipse CDT 中的 macports 在 Mac 10.7.5 上使用 gcc47 编写一个小应用程序,以学习 C++11 中的新功能。我有大量的代码编译、链接和运行。当我添加对“异步”的调用时,我得到一个链接器错误。将重现链接错误的最简单的程序如下。提前感谢您的帮助。

#include <iostream>
#include <future>

using namespace std;

void bar()
{
cout << "!!!Hello World!!!" << endl;
}

int main() {
future<void> f1(async(bar));

return 0;
}

输出是:

21:56:46 **** Incremental Build of configuration Debug for project Hello ****
make all
Building file: ../src/Hello.cpp
Invoking: GCC C++ Compiler g++ -D__GXX_EXPERIMENTAL_CXX0X__ -I/opt/local/include/gcc47/c++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"src/Hello.d" -MT"src/Hello.d" -o "src/Hello.o" "../src/Hello.cpp"
Finished building: ../src/Hello.cpp
Building target: Hello
Invoking: MacOS X C++ Linker g++  -o "Hello" 
./src/Hello.o    Undefined symbols for architecture x86_64:  
"___emutls_v._ZSt11__once_call", referenced from:
      void std::call_once<void (std::__future_base::_State_base::*)(std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()>&, bool&),
std::__future_base::_State_base* const,
std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()> >,
std::reference_wrapper<bool> >(std::once_flag&, void
(std::__future_base::_State_base::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()>&, bool&),
std::__future_base::_State_base* const&&,
std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()> >&&,
std::reference_wrapper<bool>&&) in Hello.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
Hello.o   "___emutls_v._ZSt15__once_callable", referenced from:
      void std::call_once<void (std::__future_base::_State_base::*)(std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()>&, bool&),
std::__future_base::_State_base* const,
std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()> >,
std::reference_wrapper<bool> >(std::once_flag&, void
(std::__future_base::_State_base::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()>&, bool&),
std::__future_base::_State_base* const&&,
std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()> >&&,
std::reference_wrapper<bool>&&) in Hello.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
Hello.o
      void std::__once_call_impl<std::_Bind_simple<std::_Mem_fn<void (std::__future_base::_State_base::*)(std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()>&, bool&)>
(std::__future_base::_State_base*,
std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()> >,
std::reference_wrapper<bool>)> >() in Hello.o
      void std::__once_call_impl<std::_Bind_simple<std::_Mem_fn<void (std::thread::*)()> (std::reference_wrapper<std::thread>)> >() in
Hello.o ld: symbol(s) not found for architecture x86_64 collect2:
error: ld returned 1 exit status make: *** [Hello] Error 1
4

1 回答 1

0

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54806https://trac.macports.org/ticket/36093上的信息表明此错误是由于 MacPorts 版本中的错误海湾合作委员会。根据https://trac.macports.org/ticket/36093#comment:35,这已从 MacPorts 的 r98493 开始修复。如果您更新到该版本的端口存储库,然后重新安装 GCC,我希望您的问题会消失。

于 2013-02-28T10:04:07.917 回答