c++ - 如何使用 Eigen 和 Bazel 构建一个简单的 C++ 演示?

标签 c++ eigen bazel

如何使用 Eigen在使用 Bazel 构建的 C++ 项目中(版本 0.25.2)?我喜欢使用 http_archivegit_repository 获取 Eigen 依赖项。

我试过以下方法:

main.cpp

#include <iostream>
#include <Eigen/Dense>

using Eigen::MatrixXd;

int main() {
    MatrixXd m(2, 2);
    m(0, 0) = 3;
    m(1, 0) = 2.5;
    m(0, 1) = -1;
    m(1, 1) = m(1, 0) + m(0, 1);
    std::cout << m << std::endl;
}

工作区

workspace(name = "EigenDemo")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# Eigen
http_archive(
    name = "eigen",
    build_file = "//:eigen.BUILD",
    sha256 = "3a66f9bfce85aff39bc255d5a341f87336ec6f5911e8d816dd4a3fdc500f8acf",
    url = "https://bitbucket.org/eigen/eigen/get/c5e90d9.tar.gz",
)

构建

cc_binary(
    name = "EigenDemo",
    srcs = ["main.cpp"],
    deps = [
        "@eigen",
    ],
)

eigen.BUILD

# Copyright 2017 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

package(
    default_visibility = ["//visibility:public"],
)

cc_library(
    name = "eigen",
    hdrs = glob(
        ["Eigen/**"],
        exclude = [
            "Eigen/src/OrderingMethods/Amd.h",
            "Eigen/src/SparseCholesky/**",
            "Eigen/Eigen",
            "Eigen/IterativeLinearSolvers",
            "Eigen/MetisSupport",
            "Eigen/Sparse",
            "Eigen/SparseCholesky",
            "Eigen/SparseLU",
        ],
    ),
    defines = [
        "EIGEN_MPL_ONLY",
        "EIGEN_NO_DEBUG",
    ],
    includes = ["."],
)

错误输出:

INFO: Analysed target //:EigenTest (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
ERROR: /BazelDemos/EigenDemo/BUILD:1:1: C++ compilation of rule '//:EigenTest' failed (Exit 1) gcc failed: error executing command /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer '-std=c++0x' -MD -MF ... (remaining 40 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
main.cpp:2:10: fatal error: Eigen/Dense: No such file or directory
 #include <Eigen/Dense>
      ^~~~~~~~~~~~~
compilation terminated.
Target //:EigenTest failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.258s, Critical Path: 0.10s
INFO: 0 processes.
FAILED: Build did NOT complete successfully

最佳答案

WORKSPACE 文件中缺少

strip_prefix:

workspace(name = "EigenDemo")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# Eigen
http_archive(
    name = "eigen",
    build_file = "//:eigen.BUILD",
    sha256 = "3a66f9bfce85aff39bc255d5a341f87336ec6f5911e8d816dd4a3fdc500f8acf",
    url = "https://bitbucket.org/eigen/eigen/get/c5e90d9.tar.gz",
    strip_prefix="eigen-eigen-c5e90d9e764e"
)

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

相关文章:

go - Bazel Gazelle 错误 : no such package '@org_golang_x_tools//go/analysis/internal/facts' : BUILD file not found in directory

dependencies - 如何用 bazel 组装 uberjar jar ?

c++ - 如何在 Bazel 中忽略来自外部 C++ 依赖项 header 的警告

c++ - 类模板的 C++1 7's "模板参数推导可以推导局部类型吗?

c++ - 问题 :Debuging in Visual C++ 2008 for opencv C++

c++ - 理解指向函数的指针的问题

C++ Eigen 只读稀疏 block 子表达式?怎么写?

c++ - 无法使用 C++11 实例化抽象类

c++ - 将 vector 堆叠到特征矩阵中

c++ - 编译创建 Eigen::ThreadPoolDevice 对象的代码时出错