以下 cpp 代码无法在使用 g++-4.8 或 g++-4.7 的 Ubuntu 13.10 上运行。我不知道如何解决这个问题。似乎在较旧的 Ubuntu 版本(例如 13.04)上,下面的代码可以正常工作。
代码
#include <future>
#include <iostream>
int main(int argc, char** argv)
{
int step = 0;
std::cout << "step " << ++step << std::endl;
std::promise<int> promise;
std::cout << "step " << ++step << std::endl;
try
{
promise.set_value(42);
std::cout << "step " << ++step << std::endl;
}
catch (const std::system_error& ex)
{
std::cout << "Exception: " << ex.what() << "\n";
}
}
构建并运行
g++ promise_test.cpp -o promise_test -pthread -std=c++11 && ./promise_test
输出
step 1
step 2
Exception: Unknown error -1
最佳答案
bug report .提供的解决方法是 -Wl,--no-as-needed
。看这个other question .
关于c++ - Ubuntu 13.10 上的 std::promise<T>::set_value() 以 "unknown error -1"(std::system_error) 结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21138653/