Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a simple C++ demo using Eigen with bazel?

Tags:

c++

bazel

eigen

How can I use Eigen within a C++ project that is built using Bazel (version 0.25.2)? I like to fetch the Eigen dependency using http_archive or 'git_repository'.

I've tried the following:

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

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

BUILD

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

eigen.BUILD (I use the one provided by tensorflow)

Error output:

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
like image 283
Vertexwahn Avatar asked May 16 '19 16:05

Vertexwahn


People also ask

Is Bazel a compiler?

Bazel is a tool that automates software builds and tests. Supported build tasks include running compilers and linkers to produce executable programs and libraries, and assembling deployable packages for Android, iOS and other target environments.

Does Bazel use CMake?

Building CMake projectsIn BUILD. bazel , we instantiate a cmake rule which behaves similarly to a cc_library, which can then be used in a C++ rule (cc_binary in this case).

What is a build Bazel file?

A BUILD file contains several different types of instructions for Bazel. The most important type is the build rule, which tells Bazel how to build the desired outputs, such as executable binaries or libraries.


1 Answers

strip_prefix is missing in WORKSPACE file:

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"
)
like image 147
Vertexwahn Avatar answered Oct 15 '22 01:10

Vertexwahn