'Bazel can'f find my external lib header files

I'm testing bazel and now have a problem with external library.

My demo project is like this: a simple main.cpp trying to use 3rd party libary of fastcdr.

bazel-demo/
├── src
│   ├── BUILD
│   └── main.cpp
├── third_party
│   └── fastcdr
│       └── fastcdr.BUILD
└── WORKSPACE

my WORKSPACE file:

new_local_repository(
    name = "fastcdr",
    path = "/usr/local/fast-rtps/include",
    build_file = "third_party/fastcdr/fastcdr.BUILD",
    )    

it defines a new package in /usr/local/fast-rtps/include, where the fastcdr library is pre-built and installed. (Please note that this is not standard /usr/local/include)

The main BUILD is like:

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

I have added fastcdr as deps to main target.

In my fastcdr.BUILD,

cc_library(
    name = "fastcdr",
    includes = [
        ".",
    ],
    linkopts = [
        "-L/usr/local/fast-rtps/lib",
        "-lfastcdr",
    ],
    visibility = ["//visibility:public"],
)

this is the place I'm not very sure about, but it looks correct to me.

3rd party files are already there:

/usr/local/fast-rtps/
├── examples
│   └── C++
├── include
│   ├── fastcdr
│   │   ├── Cdr.h
│   │   ├── config.h
├── lib
│   ├── libfastcdr.so.1.0.7
│   └── libfastrtps.so.1.5.0
└── share

My understand is that /usr/local/fast-rtps/include is already added into the system include, so in my main.cpp, I can include the file like this:

//#include "fastrcdr/Cdr.h"

#include <fastrcdr/config.h>

int main()
{
    return 0;
}

but bazel does not compile and has an error:

src/main.cpp:4:10: fatal error: fastrcdr/config.h: No such file or directory
 #include <fastrcdr/config.h>
          ^~~~~~~~~~~~~~~~~~~

then I use --sandbox_debug to debug like this (re-formatted a little bit):

ERROR: /home/f/gitlab/bazel-demo/src/BUILD:1:10: Compiling src/main.cpp failed: (Exit 1): linux-sandbox failed: error executing command 
  (cd /home/f/.cache/bazel/_bazel_f/dc41fd263ad6b7cccaa1b7608cdfd5aa/sandbox/linux-sandbox/32/execroot/__main__ && \
  exec env - \
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin \
    PWD=/proc/self/cwd \
    TMPDIR=/tmp \
  /home/f/.cache/bazel/_bazel_f/install/64841bf12de13c7518c7ada0994bafe2/linux-sandbox -t 15 
  -w /home/f/.cache/bazel/_bazel_f/dc41fd263ad6b7cccaa1b7608cdfd5aa/sandbox/linux-sandbox/32/execroot/__main__ 
  -w /tmp -w /dev/shm -D -- /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 bazel-out/k8-fastbuild/bin/src/_objs/main/main.pic.d '-frandom-seed=bazel-out/k8-fastbuild/bin/src/_objs/main/main.pic.o' -fPIC -iquote . 
  -iquote bazel-out/k8-fastbuild/bin 
  -iquote external/bazel_tools -iquote bazel-out/k8-fastbuild/bin/external/bazel_tools 
  
  -iquote external/fastcdr -iquote bazel-out/k8-fastbuild/bin/external/fastcdr 
  -isystem external/fastcdr -isystem bazel-out/k8-fastbuild/bin/external/fastcdr 
  
  -fno-canonical-system-headers -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TIME__="redacted"' 
  -c src/main.cpp -o bazel-out/k8-fastbuild/bin/src/_objs/main/main.pic.o)

From this line

-w /home/f/.cache/bazel/_bazel_f/dc41fd263ad6b7cccaa1b7608cdfd5aa/sandbox/linux-sandbox/32/execroot/__main__ 

I know my working path.

From these two lines:

  -iquote external/fastcdr -iquote bazel-out/k8-fastbuild/bin/external/fastcdr 
  -isystem external/fastcdr -isystem bazel-out/k8-fastbuild/bin/external/fastcdr 

I know external/fastcdr & bazel-out/k8-fastbuild/bin/external/fastcdr are added into my system include.

They are relative paths, so what's the full absolute path? I assume the external and bazel-out are below my working path. I check them, neither folder has the fastcdr path.

But if I go several folder level above, there is another external folder:

dc41fd263ad6b7cccaa1b7608cdfd5aa/
├── execroot
│   └── __main__
├── external                         <- another `external` folder here
│   ├── bazel_tools -> /home/f/.cache/bazel/_bazel_f/install/64841bf12de13c7518c7ada0994bafe2/embedded_tools
│   ├── @bazel_tools.marker
│   ├── fastcdr                      <- but fastcdr is here!!!!
│   ├── @fastcdr.marker
│   ├── fastrtps
│   ├── @fastrtps.marker
│   ├── local_config_cc
├── sandbox
│   ├── inaccessibleHelperDir
│   ├── inaccessibleHelperFile
│   └── linux-sandbox                 <- the working path in bazel output, no fastcdr

I guess I'm very close to fix the problem, but what BUILD file should I change?

Thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source