My WORKSPACE
file contains this code snippet:
new_local_repository(
name = "boost",
build_file = "//bazel/third_party:boost.BUILD",
path = "C:/msys64/mingw64/include"
)
bazel/third_party:boost.BUILD
looks like that:
cc_library(
name = "boost",
includes = ["."],
visibility = ["//visibility:public"]
)
Building the main target, that depends on boost, works perfectly fine.
Now let’s remove “include” from the path
attribute in the WORKSPACE
file and add it to the includes
attribute in bazel/third_party:boost.BUILD
:
WORKSPACE
:
new_local_repository(
name = "boost",
build_file = "//bazel/third_party:boost.BUILD",
path = "C:/msys64/mingw64"
)
bazel/third_party:boost.BUILD
:
cc_library(
name = "boost",
includes = ["include"],
visibility = ["//visibility:public"]
)
Building the main target now suddenly produces this error:
ERROR: C:/users/vschaerl/projects/testbazel/testbazel/src/BUILD:1:10: Compiling src/main.cpp failed: (Exit 1): g++ failed: error executing CppCompile command (from target //src:main) C:msys64mingw64bing++ -MD -MF bazel-out/x64_windows-fastbuild/bin/src/_objs/main/main.d -frandom-seed=bazel-out/x64_windows-fastbuild/bin/src/_objs/main/main.o -iquote . -iquote ... (remaining 25 arguments skipped)
In file included from C:/msys64/mingw64/include/c++/13.2.0/ext/string_conversions.h:43,
from C:/msys64/mingw64/include/c++/13.2.0/bits/basic_string.h:4097,
from C:/msys64/mingw64/include/c++/13.2.0/string:54,
from C:/msys64/mingw64/include/c++/13.2.0/bits/locale_classes.h:40,
from C:/msys64/mingw64/include/c++/13.2.0/bits/ios_base.h:41,
from C:/msys64/mingw64/include/c++/13.2.0/streambuf:43,
from C:/msys64/mingw64/include/c++/13.2.0/bits/streambuf_iterator.h:35,
from C:/msys64/mingw64/include/c++/13.2.0/iterator:66,
from external/boost/include/boost/array.hpp:44,
from src/main.cpp:1:
C:/msys64/mingw64/include/c++/13.2.0/cstdlib:79:15: fatal error: stdlib.h: No such file or directory
79 | #include_next <stdlib.h>
| ^~~~~~~~~~
compilation terminated.
Target //src:main failed to build
Why? Shouln’t it behave just the same? I only moved “include” from path
to includes
…
By the way, I’m on Windows and defined a custom MinGW toolchain for Bazel according to this tutorial: https://github.com/EhabHelaly/bazel_mingw_toolchain
And there are three stdlib.h
files in C:msys64mingw64include:
- in C:msys64mingw64include
- in C:msys64mingw64includec++13.2.0
- in C:msys64mingw64includec++13.2.0tr1
Thanks a lot.
EDIT/ADDITIONAL INFORMATION:
This is the compiler command line generated by Bazel before moving “include” from path to includes, when everything works fine (I separated the compiler options by lines for better readability):
C:msys64mingw64bing++
-MD
-MF bazel-out/x64_windows-fastbuild/bin/src/_objs/main/main.d
-frandom-seed=bazel-out/x64_windows-fastbuild/bin/src/_objs/main/main.o
-iquote .
-iquote bazel-out/x64_windows-fastbuild/bin
-iquote external/boost
-iquote bazel-out/x64_windows-fastbuild/bin/external/boost
-iquote external/bazel_tools
-iquote bazel-out/x64_windows-fastbuild/bin/external/bazel_tools
-isystem external/boost
-isystem bazel-out/x64_windows-fastbuild/bin/external/boost
-c src/main.cpp
-o bazel-out/x64_windows-fastbuild/bin/src/_objs/main/main.o
This is the compiler command line after moving “include” from path to includes, which produces said error:
C:msys64mingw64bing++
-MD
-MF bazel-out/x64_windows-fastbuild/bin/src/_objs/main/main.d
-frandom-seed=bazel-out/x64_windows-fastbuild/bin/src/_objs/main/main.o
-iquote .
-iquote bazel-out/x64_windows-fastbuild/bin
-iquote external/boost
-iquote bazel-out/x64_windows-fastbuild/bin/external/boost
-iquote external/bazel_tools
-iquote bazel-out/x64_windows-fastbuild/bin/external/bazel_tools
-isystem external/boost/include
-isystem bazel-out/x64_windows-fastbuild/bin/external/boost/include
-c src/main.cpp
-o bazel-out/x64_windows-fastbuild/bin/src/_objs/main/main.o
I think the problem lies in -iquote external/boost
not changing. Before, external/boost
points to C:msys64mingw64include
, which is the correct path, but after, it points to C:msys64mingw64
, which is not correct. Why does Bazel not change the path in the -iquote
compiler option?
EDIT 2:
echo | gcc -Wp,-v -x c++ - -fsyntax-only
, which according to /a/19839946/18226035 shows the default GCC include directories, prints this:
ignoring nonexistent directory "C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/include"
ignoring duplicate directory "C:/msys64/mingw64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../include/c++/13.2.0"
ignoring duplicate directory "C:/msys64/mingw64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../include/c++/13.2.0/x86_64-w64-mingw32"
ignoring duplicate directory "C:/msys64/mingw64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../include/c++/13.2.0/backward"
ignoring duplicate directory "C:/msys64/mingw64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/13.2.0/include"
ignoring nonexistent directory "D:/a/msys64/mingw64/include"
ignoring nonexistent directory "/mingw64/include"
ignoring duplicate directory "C:/msys64/mingw64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/13.2.0/include-fixed"
ignoring nonexistent directory "C:/msys64/mingw64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/include"
ignoring nonexistent directory "D:/a/msys64/mingw64/include"
#include "..." search starts here:
#include <...> search starts here:
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../include/c++/13.2.0
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../include/c++/13.2.0/x86_64-w64-mingw32
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../include/c++/13.2.0/backward
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/include
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../include
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/include-fixed
End of search list.
8
I notice the compiler command line generated by Bazel after moving include
dir to includes
is that the -isystem
path has include
dir appended at the end of the original -isystem
path.
Before change
-isystem external/boost
-isystem bazel-out/x64_windows-fastbuild/bin/external/boost
After change
-isystem external/boost/include
-isystem bazel-out/x64_windows-fastbuild/bin/external/boost/include
From the documentation, adding directories to the includes
field would generate -isystem <path_to_package>/<include_entry>
flags. This is consistent with the addition of include
dir in the includes
. However, it doesn’t explain that why <path_to_package>
remains unchanged. My guess is that <path_to_package>
is configured elsewhere. To generate similar g++ command that worked, I don’t think you need to add include
in includes
while also keeping path = "C:/msys64/mingw64"
updated because it doesn’t affect the generated g++ command
WORKSPACE
:
new_local_repository(
name = "boost",
build_file = "//bazel/third_party:boost.BUILD",
path = "C:/msys64/mingw64"
)
bazel/third_party:boost.BUILD
:
cc_library(
name = "boost",
includes = ["."],
visibility = ["//visibility:public"]
)
2