当前位置:
首页 >
使用std::thread线程相关函数,-static静态编译的程序运行时的一些常见错误
发布时间:2023/12/9
58
豆豆
生活随笔
收集整理的这篇文章主要介绍了
使用std::thread线程相关函数,-static静态编译的程序运行时的一些常见错误
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
使用std::thread的应用程序,编译时如果是动态链接pthread线程库运行正常,-static静态链接时在某些平台下可能会遇到一些意外错误。如常见编译命令:g++ -std=C++11 test.c -o test -pthread
1、Segmentation fault(段错误)
2、terminate called after throwing an instance of 'std::system_error'......
第一点,检查编译选项是否链接线程库,检查链接选项是 “-pthread” 还是 “-lpthread”?
第二点,在编译选项上添加如下选项:
-Wl,--whole-archive -lpthread -Wl,--no-whole-archive作用:强制链接引用的静态库中所有符号!!编译不报错的原因,是默认编译选项只链接到静态库中第一次出现的该引用对象,而该对象可能是“weak symbol”,因此导致问题发生。采用 whole-archive 可以把每一个引用对象包含进来,不会使用前面提到的不完整实现“weak symbol”。
完整编译选项示例:
g++ -std=C++11 -Wl,--whole-archive -pthread -Wl,--no-whole-archive test.c -o test
总结
以上是生活随笔为你收集整理的使用std::thread线程相关函数,-static静态编译的程序运行时的一些常见错误的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 微信小程序,用户拒绝授权后重新授权;un
- 下一篇: Self Introduction