hexdae / toolchains_arm_gnu

ARM embedded toolchains for Bazel
https://asnaghi.me/post/embedded-bazel/
MIT License
101 stars 35 forks source link

Failed to include C++ header <string> #29

Closed Sayter99 closed 6 months ago

Sayter99 commented 6 months ago

Hi there,

I am trying to include c++ header in a target using arm-none-eabi-g++, however, while build the target, the compiler shows an error regarding stdlib.h not found.

I tried to dig it a little bit and feel the problem might be the implementation detail #include_next "stdlib.h" that has assumptions to system include paths(?), but I had no idea how to fix it. If anyone has an idea, please help me out. Thanks in advance!

To reproduce the error:

cc_binary(
    name = "test",
    srcs = ["test.cpp"],
    target_compatible_with = [
        "@platforms//cpu:arm",
        "@platforms//os:none",
    ],
)

test.cpp

#include <string>

int main(int argc, char** argv) {
    return 0;
}

Log

ERROR: /home/sayter/inno_ws/inno_flying/ja3_io/BUILD:71:10: Compiling ja3_io/test.cpp failed: (Exit 1): arm-none-eabi-g++ failed: error executing CppCompile command (from target //ja3_io:test) 
  (cd /home/sayter/.cache/bazel/_bazel_sayter/385b308aea74aa06d0b463bbc75713c8/sandbox/linux-sandbox/2/execroot/_main && \
  exec env - \
    LD_LIBRARY_PATH=/usr/local/cuda-12.3/lib64:/usr/local/cuda-12.3/lib64: \
    PATH='/home/sayter/.cache/bazelisk/downloads/sha256/1ca5d63a30244788ff9a5b7c35584f57111a68c82bbff2929ec7b2e99f35c7a8/bin:/home/sayter/.local/bin:/usr/local/cuda-12.3/bin:/home/sayter/tools/bazel:/home/sayter/.vscode-server/bin/903b1e9d8990623e3d7da1df3d33db3e42d80eda/bin/remote-cli:/home/sayter/.local/bin:/home/sayter/.local/bin:/usr/local/cuda-12.3/bin:/home/sayter/tools/bazel:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.3/bin:/mnt/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v12.3/libnvvp:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Program Files/NVIDIA Corporation/Nsight Compute 2023.3.1/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Program Files (x86)/Windows Kits/10/Windows Performance Toolkit/:/mnt/c/Program Files/dotnet/:/mnt/c/Program Files/Git/cmd:/mnt/c/Users/ASUS/AppData/Roaming/nvm:/mnt/c/Program Files/nodejs:/mnt/c/Users/ASUS/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/ASUS/AppData/Local/Programs/Microsoft VS Code/bin:/mnt/c/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.38.33130/bin/Hostx64/x64:/mnt/c/Users/ASUS/.dotnet/tools:/mnt/c/Users/ASUS/Documents/bazelisk:/mnt/c/Users/ASUS/Documents/pthread/dll/x64:/snap/bin' \
    PWD=/proc/self/cwd \
  external/arm_none_eabi~override~arm_none_eabi~arm_none_eabi_linux_x86_64/bin/arm-none-eabi-g++ -MD -MF bazel-out/k8-fastbuild/bin/ja3_io/_objs/test/test.d '-frandom-seed=bazel-out/k8-fastbuild/bin/ja3_io/_objs/test/test.o' -iquote . -iquote bazel-out/k8-fastbuild/bin -iquote external/bazel_tools -iquote bazel-out/k8-fastbuild/bin/external/bazel_tools -Iexternal/arm_none_eabi~override~arm_none_eabi~arm_none_eabi_linux_x86_64/arm-none-eabi/include -Iexternal/arm_none_eabi~override~arm_none_eabi~arm_none_eabi_linux_x86_64/lib/gcc/arm-none-eabi/13.2.1/include -Iexternal/arm_none_eabi~override~arm_none_eabi~arm_none_eabi_linux_x86_64/lib/gcc/arm-none-eabi/13.2.1/include-fixed -Iexternal/arm_none_eabi~override~arm_none_eabi~arm_none_eabi_linux_x86_64/arm-none-eabi/include/c++/13.2.1 -Iexternal/arm_none_eabi~override~arm_none_eabi~arm_none_eabi_linux_x86_64/arm-none-eabi/include/c++/13.2.1/arm-none-eabi -fno-canonical-system-headers -no-canonical-prefixes -nostdinc '-std=gnu++14' '-mcpu=cortex-m4' -mthumb '-mfpu=fpv4-sp-d16' '-mfloat-abi=hard' -c ja3_io/test.cpp -o bazel-out/k8-fastbuild/bin/ja3_io/_objs/test/test.o)
# Configuration: cf3f7142f3edbf5a5803cb1cdf23058ecc8e679a96f8b8945ca9dd69e1f2c270
# Execution platform: @@local_config_platform//:host

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
In file included from external/arm_none_eabi~override~arm_none_eabi~arm_none_eabi_linux_x86_64/arm-none-eabi/include/c++/13.2.1/ext/string_conversions.h:43,
                 from external/arm_none_eabi~override~arm_none_eabi~arm_none_eabi_linux_x86_64/arm-none-eabi/include/c++/13.2.1/bits/basic_string.h:4097,
                 from external/arm_none_eabi~override~arm_none_eabi~arm_none_eabi_linux_x86_64/arm-none-eabi/include/c++/13.2.1/string:54,
                 from ja3_io/test.cpp:1:
external/arm_none_eabi~override~arm_none_eabi~arm_none_eabi_linux_x86_64/arm-none-eabi/include/c++/13.2.1/cstdlib:79:15: fatal error: stdlib.h: No such file or directory
   79 | #include_next <stdlib.h>
      |               ^~~~~~~~~~
compilation terminated.
Target //ja3_io:test failed to build
Sayter99 commented 6 months ago

Made it work by commenting out -nostdinc in config.bzl

RyanDraves commented 6 months ago

I had this issue as well with a library including <algorithm>. @hexdae would you accept a PR that removes that flag or makes it configurable? I'm a bit out of my depth on the pros/cons of having it

hexdae commented 6 months ago

Yes we can remove it, but in most embedded environment there are some standard functions like malloc that require standard library implementations.

In general making it opt in might be the easier choice for C++