在Windows 10和Visual Studio Community 2019下使用Bazel与Boost

6
我已经编写了一个简单的C++程序,其中使用了boost文件系统模块。我使用的构建工具是Bazel 0.25.0,并且在Windows 10 x64下进行工作。我安装了Visual Studio 2019 Community Edition,并将BAZEL_VC设置为E:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC。我还安装了MSYS2 shell。

以下是我的文件(也可在GitHub上找到):

WORKSPACE

workspace(name = "BoostFilesystemDemo")

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

# Boost
git_repository(
    name = "com_github_nelhage_rules_boost",
    commit = "6681419da0163d097d4e09c0756c0d8b785d2aa8",
    remote = "https://github.com/nelhage/rules_boost",
    shallow_since = "1556401984 -0700"
)

load("@com_github_nelhage_rules_boost//:boost/boost.bzl", "boost_deps")
boost_deps()

main.cpp

#include <iostream>
#include <boost/filesystem.hpp>

using namespace boost::filesystem;

int main(int argc, char* argv[])
{
    if (argc < 2)
    {
        std::cout << "Usage: tut1 path\n";
        return 1;
    }
    std::cout << argv[1] << " " << file_size(argv[1]) << '\n';
    return 0;
}

构建

cc_binary(
    name = "FilesystemTest",
    srcs = ["main.cpp"],
    deps = [
            "@boost//:filesystem",
    ],
)

当我尝试构建时,我会收到以下错误消息(不幸的是夹杂着一些德语 - Datei kann nicht gefunden werden 意思是文件未找到

PS E:\dev\BazelDemos\BoostFilesystemDemo> bazel build //...
INFO: Analyzed target //:FilesystemTest (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
ERROR: E:/dev/bazeldemos/boostfilesystemdemo/BUILD:1:1: Linking of rule '//:FilesystemTest' failed (Exit 1104)
LINK : fatal error LNK1104: Datei "libboost_filesystem-vc141-mt-x64-1_68.lib" kann nicht ge÷ffnet werden.
Target //:FilesystemTest failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 1.175s, Critical Path: 0.12s
INFO: 0 processes.
FAILED: Build did NOT complete successfully

有人有什么想法解决这个问题吗(编译源代码时使用Bazel 0.25.0或更高版本、Visual Studio 2019社区版、Windows 10 x64,目标应为x64)?在Ubuntu 18.04上一切顺利。

对我来说,切换到另一个提供boost的git存储库也没有问题。

我还想使用boost signals2、boost log、boost algorithm和boost compute等boost库的其他部分。


2
你的 MSVC 版本是什么?我可以使用 VC 14.0(Visual Studio 2015)进行编译。在运行 Bazel 之前,我会设置 BAZEL_VC=c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC。然而,我的构建失败了,出现了链接错误(LINK : fatal error LNK1104: cannot open file 'libboost_filesystem-vc140-mt-x64-1_68.lib')。 - László
@László:我已经切换到VS 2019并更新了我的问题。现在我有相同的错误(vc141而不是vc140)。 - Vertexwahn
1
你能发一下编译时加上 --verbose_failures 标志的输出吗?这将有助于我们诊断问题,无需复制你的设置。 - Martin Prazak
-1: 发布悬赏却没有自己尝试解决问题。你是否已经查看以下内容:1. lib文件是否存在,2. 如果存在,请检查是否有访问限制(如果可以,请尝试以管理员身份运行),3. 检查是否有一些定义在其他地方可能引起错误的路径变量(检查实际使用的路径,即检查搜索路径)。 - darune
你可以尝试使用<filesystem>而不是<boost/filesystem.hpp>来避开这个问题。 - Ted Lyngmo
@TedLyngmo 我想要使用boost库的其他部分,例如boost signals2、boost log、boost algorithm。 - Vertexwahn
1个回答

0

BUILD.boost 进行如下修改:

diff --git a/BUILD.boost b/BUILD.boost
index a3a2195..2cffdda 100644
--- a/BUILD.boost
+++ b/BUILD.boost
@@ -623,6 +623,14 @@ boost_library(
         ":system",
         ":type_traits",
     ],
+    defines = select({
+        ":linux_arm": [],
+        ":linux_x86_64": [],
+        ":osx_x86_64": [],
+        ":windows_x86_64": [
+            "BOOST_ALL_NO_LIB",
+        ],
+    }),
 )

 boost_library(
@@ -1491,6 +1499,14 @@ boost_library(
         ":predef",
         ":utility",
     ],
+    defines = select({
+        ":linux_arm": [],
+        ":linux_x86_64": [],
+        ":osx_x86_64": [],
+        ":windows_x86_64": [q
+            "BOOST_ALL_NO_LIB",

我克隆了rules_boost存储库并应用了上述更改 - 克隆的存储库可以直接在WORKSPACE文件中使用:

workspace(name = "BoostFilesystemDemo")

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

# Boost
git_repository(
    name = "com_github_nelhage_rules_boost",
    commit = "f0d2a15d6dd5f0667cdaa6da9565ccf87b84c468",
    remote = "https://github.com/Vertexwahn/rules_boost",
    shallow_since = "1557766870 +0200"
)

load("@com_github_nelhage_rules_boost//:boost/boost.bzl", "boost_deps")
boost_deps()

目前正在进行一个拉取请求,将这些更改合并到原始存储库中:https://github.com/nelhage/rules_boost/pull/123


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接