vscode 使用c11标准库也报错threads.h: No such file or directory
时间: 2023-10-30 22:07:09 浏览: 55
如果在 VS Code 中使用 C11 标准库时遇到了 `threads.h: No such file or directory` 的错误,这可能是因为您使用的编译器不支持 C11 标准库中的线程库。
在 Windows 上,GCC 默认不包括 C11 标准库中的线程库。如果您要使用 C11 标准库中的线程库,请使用支持线程库的编译器,例如 Clang 或 Microsoft Visual C++。
以下是使用 Microsoft Visual C++ 的示例代码:
```c
#include <stdio.h>
#include <threads.h>
int thread_func(void* arg) {
printf("Hello from thread!\n");
return 0;
}
int main() {
thrd_t t;
thrd_create(&t, thread_func, NULL);
thrd_join(t, NULL);
return 0;
}
```
请注意,您需要使用 `/std:c11` 编译器选项启用 C11 标准库支持。在 VS Code 中,您可以在 `tasks.json` 文件中添加以下选项来编译和链接您的代码:
```json
{
"tasks": [
{
"label": "build",
"type": "shell",
"command": "cl.exe /std:c11 ${file} /Fe${fileBasenameNoExtension}.exe",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
```
请将上面的代码示例保存在名为 `thread_example.c` 的文件中,并使用上面的任务来编译和运行它。