2

Using Bazel 2.2, how I can I build against external library, for example in my case I would like to build against boost (pre-built for MS VC++ 2019) , the question is this possible in Bazel?

Given that the local path to boost library is c:\boost_1_72_0, in which there are three folders bin, include and lib

If so how is it possible to tell the compiler and linker:

  • path to include files
  • path to lib files
  • boost library specific .lib files to link (i.e. boost_context-vc142-mt-x64-1_72.lib)

I've tried the below cc_library but unfortunately it didn't work.

cc_library(
    name = "boost",
    srcs = glob(["*.lib"]),
    hdrs = glob(["*.hpp", "*.*", "*"] + ["boost/*.hpp"] + ["boost/*/*.hpp"]),
    includes = [
        "C:/boost_1_72_0/include"
    ],
    linkopts = ["-pthread","-LC:/boost_1_72_0/lib"],
    visibility = ["//visibility:public"],
)

cc_binary(
    name = "hello-bazel",
    srcs = ["main.cpp", "SomeClass.h", "SomeClass.cpp"],
    deps = [":boost"],
)
  • Not sure I follow, your external lib should be a `cc_library` target and be added to your `main` as one of the `deps`, or? – Ondrej K. Mar 06 '20 at 18:23
  • @OndrejK. yes it should be added as dep to main target – mohamed selim Mar 09 '20 at 08:19
  • What I meant is, I would expect you have an external dependency, for what you're saying perhaps [`new_local_repository`](https://docs.bazel.build/versions/master/be/workspace.html#new_local_repository) by the sound of it, and you use its `path` attribute and in the `BUILD` you provide through `build_file[_content]` you define a cc_library you use for `deps` in another `cc_*` rule. Question for `VAR_PATH` sounds a little dubious and perhaps not actually what you should be after? TL;DR: instead of asking how to write cmake in bazel, explain a bit more your setup and what you're trying to do. – Ondrej K. Mar 09 '20 at 19:07
  • @OndrejK.how about this, I've rephrased the question, hope it's clear right now! – mohamed selim Mar 12 '20 at 10:15
  • thanks @OndrejK. it was new_local_repository that solved my question. – mohamed selim Mar 16 '20 at 17:15

1 Answers1

2

Finally I've figured it out. Actually it's very different from CMake or any other build tool, in case you're coming from CMake background (just like me).

First of all I assume that you do have a pre-built c/c++ external library i.e. C:\boost with include, bin and lib folder structures, also suppose you do have two more important things:

  • folder that hosts your main WORKSPACE file.
  • BUILD file that refers to your main application(package)

The main build file should be:

cc_binary(
    name = "hello-bazel",
    srcs = ["main.cpp", "SomeClass.h", "SomeClass.cpp"],
    deps = ["@boost//:boost_172_shared"],
)

We need to add another build file this time let's name it BUILD.boost place inside sub-folder to your app's main folder i.e. hello-bazel/dependency so as to look like as follows:

hello-bazel -> example folder

  • WORKSPACE -> file
  • main -> folder that hosts your BUILD, main.cpp, someclass.h and someclass.cpp
  • dependency -> folder that hosts BUILD.boost

Add BUILD.boost as pointed above in the sub-folder dependency to include:

cc_library(
    name = "boost_172_shared",
    srcs = glob(["lib/*.lib"]),
    hdrs = glob( ["include/boost/*.hpp"] +    ["include/boost/*.h"] + 
                 ["include/boost/**/*.hpp"] + ["include/boost/**/*.h"] + 
                 ["include/boost/**/**/**/*.hpp"] + ["include/boost/**/**/**/*.h"] + 
                 ["include/boost/**/**/*.hpp"] +    ["include/boost/**/**/*.h"] ),
    strip_include_prefix = "include/",
    visibility = ["//visibility:public"]
)

We need to edit your WORKSPACE file as shown above to include the following:

new_local_repository(
    name = "boost",
    path = "C:\\Development\\Libraries\\boost\\",
    build_file = "dependency\\BUILD.boost",
)

The above new_local_repository is the key thing that points to your external lib as mentioned by – Ondrej K in the comments.

  • I am trying to do the same thing, but for some reason when I bazel run the binray target, it segfaults. My post is here: https://stackoverflow.com/questions/63158439/bazel-run-immediately-segmentation-faults. If you have any ideas please let me know! – cmoses Jul 29 '20 at 16:52