简体   繁体   中英

Bazel how to build a simple application that links a pre-built external Library

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 (ie 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"],
)

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 ie 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 ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM