[英]How to programmatically detect whether UAC is enabled in Windows 8 using C++?
[英]How to use select to properly detect whether I am building C++ code in Windows or Linux?
我正在编写一个示例 C++ 项目,该项目使用 Bazel 作为其他合作者可以遵循的示例习语。
这是存储库: https://github.com/thinlizzy/bazelexample
我很想知道我做的是否“正确”,更具体地说是关于这个文件: https://github.com/thinlizzy/bazelexample/blob/38cc07931e58ff5a888dd6a83456970f76d7e5b3/demo/BUILD在选择特定实现时。
cc_library(
name = "demo",
srcs = ["demo.cpp"],
deps = [
"//example:frontend",
],
)
cc_binary(
name = "main_win",
deps = [
":demo",
"//example:impl_win",
],
)
cc_binary(
name = "main_linux",
deps = [
":demo",
"//example:impl_linux",
],
)
这是否遵循 Bazel 项目的正确/预期习惯用法? 我已经为其他项目这样做了,通过将所有特定于平台的依赖项集中在单独的目标中,然后二进制文件只依赖于它们。
bazel-discuss 列表中有人告诉我改用select ,但我的尝试未能“检测到”操作系统。 我确定我做错了什么,但是缺乏信息和示例并不能告诉我如何正确使用它。
@bazel_tools
包含预定义的平台条件:
$ bazel query @bazel_tools//src/conditions:all
@bazel_tools//src/conditions:windows_msys
@bazel_tools//src/conditions:windows_msvc
@bazel_tools//src/conditions:windows
@bazel_tools//src/conditions:remote
@bazel_tools//src/conditions:host_windows_msys
@bazel_tools//src/conditions:host_windows_msvc
@bazel_tools//src/conditions:host_windows
@bazel_tools//src/conditions:freebsd
@bazel_tools//src/conditions:darwin_x86_64
@bazel_tools//src/conditions:darwin
您可以直接在BUILD
文件中使用它们:
cc_library(
name = "impl",
srcs = ["Implementation.cpp"] + select({
"@bazel_tools//src/conditions:windows": ["ImplementationWin.cpp"],
"@bazel_tools//src/conditions:darwin": ["ImplementationMacOS.cpp"],
"//conditions:default": ["ImplementationLinux.cpp"],
}),
# .. same for hdrs and data
)
cc_binary(
name = "demo",
deps = [":impl"],
)
有关语法的详细信息,请参阅select
的文档。
将.bazelrc
添加到您的项目中。 添加行build:vs2019 --cxxopt=/std:c++14
和build:gcc --cxxopt=-std=c++14
。 构建您的代码bazel build --config=msvc //...
或bazel build --config=gcc //....
@Vertexwahn 的回答让我有些困惑,所以我希望这个回答有助于澄清一点。 虽然他的回答与问题没有直接关系,但它可能对其他试图在没有特定文件包含的完全不同平台上构建的人有用。
这是我回答该特定问题的链接: How do I specify portable build configurations for different operating systems for Bazel?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.