abu*_*uss 9 c++ static-libraries c++20
我正在开发一个std::jthread使用 g++ 10.0.1的库(C++20 中的新功能)。如果我使用共享库编译它,该库工作正常,但如果我使用静态库编译它,我会在程序结束时遇到分段错误(线程析构函数)。我已经将我的测试用例缩小到一个简单的线程创建和连接:
#include <iostream>
#include <thread>
int main(int argc, const char** argv) {
auto t = std::jthread([]() { std::cout << "OK\n"; });
t.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
要编译我使用:
g++-10 -o test --static -std=c++20 test.cc -lpthread
Run Code Online (Sandbox Code Playgroud)
并运行它:
% ./test
zsh: segmentation fault (core dumped) ./test
Run Code Online (Sandbox Code Playgroud)
有没有人知道这个问题可能是什么?
更新:
按照@JérômeRichard 建议的参考,我能够毫无问题地编译和运行我的小测试程序
g++-10 -o test --static -std=c++20 test.cc -lrt -pthread -Wl,--whole-archive -lpthread -Wl,--no-whole-archive
Run Code Online (Sandbox Code Playgroud)
但是,将代码更改为使用request_stop而不是join程序会再次分段错误(https://godbolt.org/z/obGN8Y)。
#include <iostream>
#include <thread>
int main() {
auto t = std::jthread([]{ std::cout << "OK" << std::endl; });
t.request_stop();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
212 次 |
| 最近记录: |