[英]bazel: cannot find dependent header file for very simple case
首先,我的環境:
我使用 bazel 做了一個非常簡單的 C++ 項目。 它有兩個BUILD
來測試目標引用。 布局是:
- WORKSPACE
- libfoo
|- BUILD
|- foo.h
|- foo.cpp
- bar
|- BUILD
|- bar.cpp
在libfoo的BUILD
文件中,定義了一個非常簡單的庫:
cc_library(
name = "foo",
srcs = ["foo.cpp"],
hdrs = ["foo.h"],
visibility = ["//visibility:public"]
)
在 bar 的BUILD
文件中,它聲明了一個 deps libfoo 的可執行文件:
cc_binary(
name = "bar",
srcs = ["bar.cpp"],
deps = ["//libfoo:foo"],
)
,其中bar.cpp
調用了 libfoo 中定義的函數:
#include "foo.h"
#include <iostream>
int main()
{
std::clog << "bar main" << std::endl;
say_foo(); // a function defined in libfoo
}
但是,當我使用bazel build "//bar:bar"
編譯bazel build "//bar:bar"
,編譯器聲稱foo.h
無法打開(錯誤代碼C1083 )。
你有兩種解決方法:
您可以在cc_library
指定includes
屬性
cc_library(
name = "foo",
srcs = ["foo.cpp"],
hdrs = ["foo.h"],
includes = ["./"],
visibility = ["//visibility:public"]
)
或者你在bar.cpp
包含foo.h
作為#include "libfoo/foo.h"
。
詳細查看官方教程后,我發現我誤解了 deps 的功能,它沒有將 library 的 dir 添加到inc dir 中。
實際上, #include "libfoo/foo.h"
應該寫在bar.cpp
而不是#include "foo.h"
,其中必須使用目標 foo 的完整相對路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.