0

I was able to install glog with:

brew install glog

Then I can successfully compile and use it using g++:

g++ src/main/main_logger.cc -std=c++17 -lglog

How can I do this with bazel?

I get this error:

fatal error: 'glog/logging.h' file not found
#include <glog/logging.h>
     ^~~~~~~~~~~~~~~~
1 error generated.

UPDATE:

Instead of installing and building glog locallay, I ended up referencing it as a git repo in the WORKSPACE file:

git_repository(
    name = "glog",
    remote = "https://github.com/google/glog.git",
    tag = "v0.5.0",
)

Now I can depend on it in my cc_binary rules like this:

cc_binary(
    name = "main_logger",
    srcs = ["main_logger.cc"],
    deps = [
        "//src/lib:CPPLib",
        "@com_github_gflags_gflags//:gflags",
        "@glog",
    ],
)

Complete example here.

Ari
  • 7,251
  • 11
  • 40
  • 70
  • You need to also set the `-I`nclude path to include wherever you put the library's headers. – underscore_d Jun 11 '20 at 14:50
  • I don't need `-I` with g++. It just works. – Ari Jun 11 '20 at 14:55
  • What do you mean? You ran `bazel src/main/main_logger.cc -std=c++17 -lglog`? For that I would expect an entirely different error message (about `src/main/main_logger.cc` not being known). You may need to elaborate a bit more what you're trying to do and what have you done trying to get it. Generally, you want to declare an [external dependency](https://docs.bazel.build/versions/master/external.html) for use as `deps` of code you're trying to build in your tree. Alternatively, you can rely on the host configuration in which case you need to make sure your toolchain is configured correctly. – Ondrej K. Jun 11 '20 at 17:40

1 Answers1

1

There is already a doc about to use glog within a project which uses the Bazel build tool. link

Then you can create a BUILD file, and use bazel build //src/main:main_logger to build it.

cc_binary(
    name = "main_logger",
    srcs = ["main_logger.cc"],
    deps = ["@com_github_google_glog//:glog"],
)
daohu527
  • 452
  • 4
  • 15