如何使用Eigen和bazel构建简单的C ++演示?

时间:2019-05-16 16:07:44

标签: c++ eigen bazel

如何在使用Eigen(0.25.2版)构建的C ++项目中使用Bazel?我喜欢使用http_archive或'git_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"],
    copts = ["-Iexternal/eigen"],
    deps = [
        "@eigen",
    ],
)

eigen.BUILD(我使用张量流提供的那个)

错误输出:

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

2 个答案:

答案 0 :(得分:4)

strip_prefixWORKSPACE文件中丢失:

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"
)

答案 1 :(得分:0)

Tensorflow 不再使用单独的 eigen.BUILD 文件。相反,您可以将以下内容放入您的 WORKSPACE 文件中(无需单独的 eigen.BUILD 文件):

http_archive(
    name = "com_gitlab_libeigen_eigen",
    sha256 = "0215c6593c4ee9f1f7f28238c4e8995584ebf3b556e9dbf933d84feb98d5b9ef",
    strip_prefix = "eigen-3.3.8",
    urls = [
        "https://gitlab.com/libeigen/eigen/-/archive/3.3.8/eigen-3.3.8.tar.bz2",
    ],
    build_file_content =
"""
# TODO(keir): Replace this with a better version, like from TensorFlow.
# See https://github.com/ceres-solver/ceres-solver/issues/337.
cc_library(
    name = 'eigen',
    srcs = [],
    includes = ['.'],
    hdrs = glob(['Eigen/**']),
    visibility = ['//visibility:public'],
)
"""
)

(正如您可能从 TODO 中得知的那样,我从 Ceres 撕下了这个。)