为什么这段代码不能用 clang 构建,用 gcc 崩溃,但在 VC++ 上运行良好?
Why this code doesn't build with clang, crashes with gcc, but runs fine with VC++?
此代码:
#include <iostream>
#include <future>
int main()
{
std::async([]{std::cout << "hellon";}).get();
}
运行良好,使用 Visual C++ 2013 打印hello
,但在 gcc 生成时引发异常:
terminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1
甚至不使用 clang 构建,产生以下错误消息:
/usr/bin/ld: /tmp/source-83f28e.o: undefined reference to symbol 'pthread_setspecific@@GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我使用 rexter 来运行测试。你能解释一下这种行为吗?
编辑
使用-pthread
编译器选项,gcc版本运行良好,clang版本现在构建并产生:
hello
exception_ptr not yet implemented
为了添加对多线程的支持,您需要链接到 pthread 库。这是在GCC和Clang上-pthread
(作为内置标志提供)。或者,-lpthread
可以做到这一点。
相关文章:
- C++ Python 模块在 Blender 中崩溃,但在 Python 控制台中不会崩溃
- Mac os x app bundle 在下载并运行时崩溃,但在终端或更改 Info.plist 时运行良好
- 在C++中调用 free() 会触发 ntdll!DbgBreakPoint() 在调试中,但在发布中崩溃
- C++ rezing 动态数组最多可以处理一定数量的元素,但在某些时候会崩溃并显示错误 (0XC0000005)
- 简单的文本文件格式化在linux下崩溃,但在Windows中很好
- cv::split() 在至强处理器上崩溃,但在其他地方工作
- "Unable to open file libc++abi.dylib" .程序生成,但在运行时崩溃。使用 xcode 10.1 (10B61)
- 一般来说,使用Qt Creator,是什么导致程序在调试模式下编译时正确运行,但在发布模式下崩溃
- 我的程序在Windows机器上崩溃了,但在Linux上运行良好
- 为什么对齐的参数编译正确,但在运行时崩溃
- DBUS 代码在放置在守护进程内时崩溃,但在没有守护进程代码的独立独立 main() 函数中运行良好
- 在 Xcode 6 中运行 cpp-netlib - 编译但在运行时崩溃
- 为什么我的.exe文件崩溃了,但在调试和发布版本中工作得很好
- Simplecrypt让我的QT应用程序在OS X Mountain Lion上崩溃,但在Windows上完美运行
- 非常简单的C++代码在Android上崩溃,但在OS X上运行良好
- 为什么在VS 2010 C++中,我的程序在调试器中运行良好时会崩溃,但在没有调试器的情况下运行时会崩溃
- OpenCV简单代码编译成功,但在运行时崩溃
- GLEW编译,但在运行时崩溃
- openCV程序可编译,但在运行时崩溃
- PDB无法显示发布版本崩溃的确切行号,但在二进制调试版本的情况下显示精确行号