Reputation: 71
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:
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"],
)
Upvotes: 3
Views: 3408
Reputation: 71
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:
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
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.
Upvotes: 2