Problems with absolute paths for includes in C++ project

4,006 views
Skip to first unread message

compl...@gmail.com

unread,
Mar 15, 2018, 2:22:43 PM3/15/18
to bazel-discuss
I'm fairly new to Bazel, having moved here from Buck recently after finding Buck lacking in certain key areas. I've been trying to move a few projects from being Make-based to using Bazel instead, and am having trouble with including third-party libraries in my project.

Near as I can tell, it seems like there are two ways to add include directives in Bazel. The first is to add elements to the "includes" property, and the second is to add include directives to the "copts" property. If I try adding absolute paths to the "includes" property however, I see an error like this:

in includes attribute of cc_library rule //:foo: ignoring invalid absolute path '/usr/local/include'

This is clearly a deliberate error, and rather than argue its merits, let's set it aside and instead try out our second option. My "copts" property looks like this:

copts = [
'-I/usr/local/include',
'-I/usr/local/opt/libressl/include',
]

Trying to build with things set this way gives the following error:

in cc_library rule //:foo: The include path '/usr/local/opt/libressl/include' references a path outside of the execution root.

Note the lack of a message concerning /usr/local/include. If I comment out only the libressl line, the build runs just fine, and I just get compiler errors:

src/foo.cpp:4:10: fatal error: 'openssl/bio.h' file not found

(this is all with Buck v2017.11.16.01 installed via homebrew on MacOS)

Since the treatment of absolute paths in "copts" is uneven, I can only conclude that there's a bug here. After some googling it sounds like this has been reported before, multiple times, but those tickets are all closed. So what's the status? Is this fixed already in a branch and simply not available in a release yet?

Shimin Guo

unread,
Mar 15, 2018, 2:32:24 PM3/15/18
to compl...@gmail.com, bazel-discuss
I had the same problem. I solved it by using a workspace rule to pull in the headers.

# WORKSPACE
new_local_repository(
    name = "myheaders",
    path = "/path/to/myheaders",
    build_file_content = """
package(
    default_visibility = [
        "//visibility:public",
    ],
)

cc_library(
    name = "headers",
    srcs = glob(["**/*.h"]),
)
""",
)

# BUILD.bazel
cc_library(
   ...
    deps = [
        "@myheaders//:headers",
    ],
    ...
)



--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/fb5a69bf-d9cf-4c5d-bb1c-7c13816e4976%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Shimin Guo

unread,
Mar 15, 2018, 2:36:27 PM3/15/18
to compl...@gmail.com, bazel-discuss
oh, you also need -Iexternal/myheaders in your copts.

compl...@gmail.com

unread,
Mar 15, 2018, 4:12:49 PM3/15/18
to bazel-discuss
Thanks! That worked perfectly.
Reply all
Reply to author
Forward
0 new messages