gpt4 book ai didi

c++ - 我如何使用 Bazel 构建这个简单的示例?

转载 作者:行者123 更新时间:2023-11-30 04:50:10 27 4
gpt4 key购买 nike

假设我有这样一个项目:

$ tree . 
├── WORKSPACE
├── include
│   └── header.hpp
└── main.cpp
└── BUILD.bazel

main.cpp 看起来像这样:

#include "header.hpp"

int main() {
return 0;
}

我的 BUILD.bazel 文件应该是什么样的?

我目前的尝试:

cc_binary(
name = "app",
srcs = [
"main.cpp",
"include/header.hpp",
],
)

编辑:忘记提及我的 WORKSPACE 文件


编辑:找到了一个解决方法,但我认为它不是很优雅:

cc_library(
name = "app-hdrs",
hdrs = [
"include/header.hpp",
],
srcs = [
"include/header.hpp",
],
strip_include_prefix = "include",
)

cc_binary(
name = "app",
srcs = [
"main.cpp",
],
deps = [
":app-hdrs",
],
)

最佳答案

您的项目文件夹中需要一个名为 WORKSPACE 的文件:

$ tree . 
├── include
│ └── header.hpp
└── main.cpp
└── BUILD.bazel
└── WORKSPACE

然后您可以使用以下命令构建您的应用:

bazel 构建//:app

并且还在 copts-flag 中指定包含路径:

cc_binary(
name = "app",
srcs = [
"main.cpp",
"include/header.hpp",
],
copts = ["-Iinclude", "-Wall", "-Werror"],
)

cc_binary(
name = "app",
includes = [ "include" ],
srcs = [
"main.cpp",
"include/header.hpp",
],
copts = [ "-Wall", "-Werror" ],
)

关于c++ - 我如何使用 Bazel 构建这个简单的示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55101352/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com