我有以下构建文件:
cc_binary(
name = "main",
srcs = ["main.cpp"],
copts = [
'-fpic',
'-pthread'
],
)
使用以下 main.cpp:
#include <thread>
int main(int argc, char *argv[])
{
auto a = std::thread([](){});
return 0;
}
上面的 cpp 文件使用 g++ -pthread
编译,但是在 Bazel 中编译时失败并出现以下错误:
/usr/include/c++/7/thread:122: error: undefined reference to 'pthread_create'
我是否以错误的方式传递了旗帜?在使用 Bazel 进行编译时,我需要做什么才能拥有线程?
编辑:
-pthread
和 -lpthread
都不起作用
最佳答案
-lpthread
需要添加到 linkopts
属性中(如 King Stone 和 dms 所建议的那样)
cc_binary(
name = "main",
srcs = ["main.cpp"],
copts = [
"-fpic",
],
linkopts = ["-lpthread"],
)
error: undefined reference
是链接器错误。 linkopts
属性中指定的所有命令都被移交给链接器。
关于c++ - 如何使用 Bazel 编译一个简单的线程应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58720518/