sdgfsdh
sdgfsdh

Reputation: 37045

How do I build this simple example with Bazel?

Suppose I have a project like this:

$ tree . 
├── WORKSPACE
├── include
│   └── header.hpp
└── main.cpp
└── BUILD.bazel

And main.cpp looks like this:

#include "header.hpp"

int main() {
  return 0;
}

What should my BUILD.bazel file look like?

My current attempt:

cc_binary(
  name = "app",
  srcs = [
    "main.cpp",
    "include/header.hpp",
  ],
)

Edit: Forgot to mention my WORKSPACE file


Edit: Found a work-around, but I don't think it is very elegant:

cc_library(
  name = "app-hdrs",
  hdrs = [
    "include/header.hpp",
  ],
  srcs = [
    "include/header.hpp",
  ],
  strip_include_prefix = "include",
)

cc_binary(
  name = "app",
  srcs = [
    "main.cpp",
  ],
  deps = [
    ":app-hdrs",
  ],
)

Upvotes: 2

Views: 2327

Answers (1)

Mike van Dyke
Mike van Dyke

Reputation: 2868

You need a file called WORKSPACE in your project folder:

$ tree . 
├── include
│   └── header.hpp
└── main.cpp
└── BUILD.bazel
└── WORKSPACE

Then you can build your app with the following commmand:

bazel build //:app

And also specify the include path in the copts-flag:

cc_binary(
  name = "app",
  srcs = [
    "main.cpp",
    "include/header.hpp",
  ],
  copts = ["-Iinclude", "-Wall", "-Werror"],
)

cc_binary(
  name = "app",
  includes = [ "include" ],
  srcs = [
    "main.cpp",
    "include/header.hpp",
  ],
  copts = [ "-Wall", "-Werror" ],
)

Upvotes: 2

Related Questions