这段代码是否有效,还是我的编译器坏了?
#include <future>
#include <iostream>
int main() {
std::cout << "doing the test" << std::endl;
std::promise<bool> mypromise;
std::future<bool> myfuture = mypromise.get_future();
mypromise.set_value(true);
bool result = myfuture.get();
std::cout << "success, result is " << result << std::endl;
return 0;
}
这是输出:
$ g++-mp-4.8 -std=c++11 test.cpp
$ ./a.out
doing the test
Segmentation fault: 11
$
我正在使用 g++-mp-4.8,它是来自 macports 的 gcc 4.8。
我要疯了吗?
最佳答案
动态链接器可能会将您的程序链接到旧版本的 libstdc++
,即 /opt/local/lib/libstdc++.6.dylib
由于您使用 GCC 4.8 进行编译,您需要使用 GCC 4.8 附带的新 libstdc++
,它可能是 /opt/local/lib/gcc48/libstdc++.6。 dylib
您应该检查 /opt/local/lib/libstdc++.6.dylib
是否是 GCC 4.8 附带的库,如果不是,请使用正确的库。
您可以通过多种方式控制它,最简单(但不一定是最好的)是运行:
export DYLD_LIBRARY_PATH=/opt/local/lib/gcc48/
./a.out
见 http://gcc.gnu.org/onlinedocs/libstdc++/faq.html#faq.how_to_set_paths和 http://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dynamic_or_shared.html#manual.intro.using.linkage.dynamic其他信息(不特定于 Mac OS X)
关于c++ - 我的机器上的 std::promise 是否损坏(使用 g++-mp)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15778085/