Windows下用Clang编译独立头文件版ASIO出现运行时错误
Windows下Clang编译独立ASIO项目的线程运行时错误问题
从Visual Studio转向命令行编译,在Windows上使用Clang编译独立(非Boost)头文件形式的ASIO项目时遇到问题:Visual Studio和Clang编译均无错误,但Clang编译的版本运行时,创建asio::thread会抛出运行时错误:thread: The attempted operation is not supported for the type of object referenced.
最小示例代码
main.cpp
#include <iostream> #include <vector> #include <string> #include <map> #define _WIN32_WINNT 0x0601 #define ASIO_STANDALONE #define STANDALONE #include <asio.hpp> void foobar() { std::cout << "B" << std::endl; } int main() { asio::io_context ctx; std::cout << "A "; try { asio::thread thr(std::bind(&foobar)); thr.join(); } catch (std::exception& e) { std::cout << e.what() << std::endl; } }
编译与运行情况
- Clang编译命令:
clang++ -std=c++20 main.cpp -I"asio-1.30.2/include" -luser32 -lkernel32 -lshell32,返回值为0 - Visual Studio:配置为空白C++应用标准设置,仅添加asio/include到包含路径,编译无问题且运行正常,输出:
A B - Clang编译版本运行输出:
A thread: The attempted operation is not supported for the type of object referenced.
猜测方向
- 未通过#define正确配置ASIO
- 程序链接配置有误
- ASIO不支持在Windows上用Clang编译独立头文件版本
内容的提问来源于stack exchange,提问作者RRKS101_1 TF2
相关产品推荐
相关产品推荐

