Skip to content

Missing stdlib.h include when C++ standard library is used. #17564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
simonmarti opened this issue Jul 16, 2019 · 9 comments · Fixed by #18242
Closed

Missing stdlib.h include when C++ standard library is used. #17564

simonmarti opened this issue Jul 16, 2019 · 9 comments · Fixed by #18242
Assignees
Labels
area: Build System area: C Library C Standard Library area: C++ bug The issue is a bug, or the PR is fixing a bug priority: medium Medium impact/importance bug
Milestone

Comments

@simonmarti
Copy link

Describe the bug

stdlib.h is not in the GCC search path when the C++ standard library is used.

To Reproduce

prj.conf:

CONFIG_NEWLIB_LIBC=y
CONFIG_CPLUSPLUS=y

main.cpp:

#include <zephyr.h>
#include <misc/printk.h>
#include <array>


void main(void)
{
	std::array<int, 3> a{ 1, 2, 3 };

	for (const auto x: a)
	{
		printk("x: %d", x);
	}
}

Steps to reproduce the behavior:

  1. mkdir build && cd build
  2. cmake -GNinja -DBOARD=arduino_zero ..
  3. ninja
  4. Build fails because of missing include, see console output below

Expected behavior

The example code above should compile.

Impact

I can not use the C++ standard library for my current project.

Screenshots or console output

> ninja
[15/101] Building CXX object CMakeFiles/app.dir/src/main.cpp.obj
FAILED: CMakeFiles/app.dir/src/main.cpp.obj 
C:\Users\username\tools\gcc-arm-none-eabi-7-2018-q2-update-win32\bin\arm-none-eabi-g++.exe  -DBUILD_VERSION=zephyr-v1.14.0 -DKERNEL -D_FORTIFY_SOURCE=2 -D__LINUX_ERRNO_EXTENSIONS__ -D__ZEPHYR__=1 -IC:/Users/username/zephyrproject/zephyr/kernel/include -IC:/Users/username/zephyrproject/zephyr/arch/arm/include -IC:/Users/username/zephyrproject/zephyr/include -IC:/Users/username/zephyrproject/zephyr/include/drivers -Izephyr/include/generated -IC:/Users/username/zephyrproject/zephyr/soc/arm/atmel_sam0/samd21 -IC:/Users/username/zephyrproject/zephyr/ext/hal/atmel/asf/sam0/include/samd21 -IC:/Users/username/zephyrproject/zephyr/ext/hal/cmsis/Include -isystem C:/Users/username/tools/gcc-arm-none-eabi-7-2018-q2-update-win32/arm-none-eabi/include -std=c++11 -Os -fcheck-new -Wno-register -fno-exceptions -fno-rtti -g -Wall -Wformat -Wformat-security -Wno-format-zero-length -imacros C:/Users/username/tmp/hello_world/build/zephyr/include/generated/autoconf.h -ffreestanding -Wno-main -fno-common -mthumb -mcpu=cortex-m0plus -fno-asynchronous-unwind-tables -fno-pie -fno-pic -fno-strict-overflow -Wno-unused-but-set-variable -fno-reorder-functions -fno-defer-pop -Werror=implicit-int -Wpointer-arith -ffunction-sections -fdata-sections -mabi=aapcs -march=armv6s-m -MD -MT CMakeFiles/app.dir/src/main.cpp.obj -MF CMakeFiles\app.dir\src\main.cpp.obj.d -o CMakeFiles/app.dir/src/main.cpp.obj -c ../src/main.cpp
In file included from c:\users\username\tools\gcc-arm-none-eabi-7-2018-q2-update-win32\arm-none-eabi\include\c++\7.3.1\ext\string_conversions.h:41:0,
                 from c:\users\username\tools\gcc-arm-none-eabi-7-2018-q2-update-win32\arm-none-eabi\include\c++\7.3.1\bits\basic_string.h:6352,
                 from c:\users\username\tools\gcc-arm-none-eabi-7-2018-q2-update-win32\arm-none-eabi\include\c++\7.3.1\string:52,
                 from c:\users\username\tools\gcc-arm-none-eabi-7-2018-q2-update-win32\arm-none-eabi\include\c++\7.3.1\stdexcept:39,
                 from c:\users\username\tools\gcc-arm-none-eabi-7-2018-q2-update-win32\arm-none-eabi\include\c++\7.3.1\array:39,
                 from ../src/main.cpp:8:
c:\users\username\tools\gcc-arm-none-eabi-7-2018-q2-update-win32\arm-none-eabi\include\c++\7.3.1\cstdlib:75:15: fatal error: stdlib.h: No such file or directory
 #include_next <stdlib.h>
               ^~~~~~~~~~
compilation terminated.
[28/101] Building C object zephyr/CMakeFiles/zephyr.dir/lib/os/printk.c.obj
ninja: build stopped: subcommand failed..

Environment (please complete the following information):

  • OS: Windows 10 1903
  • Toolchain: GNU ARM Embedded 7-2018-q2-update
  • Zephyr: v1.14.0

Additional context

This might be related to issue #15603. I tried the suggested workaround, but it does not help.

I tried several Zephyr versions:

  • v1.14.1-rc1
  • v1.14.99 (latest master, commit 1c6952c )
  • v1.14.0-rc1

v1.14.0-rc1 works but in this version the shell is not C++ compatible yet, so there are other problems to work around.

I also tried the latest GNU ARM Embedded toolchain (8-2019-q3-update), but also no success here.

If you can point my in the right direction, I will happily provide a fix myself.

@simonmarti simonmarti added the bug The issue is a bug, or the PR is fixing a bug label Jul 16, 2019
@carlescufi
Copy link
Member

carlescufi commented Jul 16, 2019

@simonmarti could you please try with the Zephyr SDK instead of GNU Arm Embedded and let us know if you can reproduce the same issue?

EDIT: I tried and the SDK doesn't seem to be able to find <array>:

FAILED: CMakeFiles/app.dir/src/main.c.obj
/home/carles/bin/zephyr-sdk/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc -DBUILD_VERSION=zephyr-v1.14.0-2394-g474c99c9ef7f -DKERNEL -DNRF52840_XXAA -D_FORTIFY_SOURCE=2 -D__LINUX_ERRNO_EXTENSIONS__ -D__ZEPHYR__=1 -I/home/carles/src/zephyr/zephyr/kernel/include -I/home/carles/src/zephyr/zephyr/arch/arm/include -I/home/carles/src/zephyr/zephyr/include -I/home/carles/src/zephyr/zephyr/include/drivers -Izephyr/include/generated -I/home/carles/src/zephyr/zephyr/soc/arm/nordic_nrf/nrf52 -I/home/carles/src/zephyr/zephyr/soc/arm/nordic_nrf/include -I/home/carles/src/zephyr/zephyr/ext/hal/cmsis/Include -I/home/carles/src/zephyr/zephyr/ext/hal/nordic/nrfx -I/home/carles/src/zephyr/zephyr/ext/hal/nordic/nrfx/drivers/include -I/home/carles/src/zephyr/zephyr/ext/hal/nordic/nrfx/hal -I/home/carles/src/zephyr/zephyr/ext/hal/nordic/nrfx/mdk -I/home/carles/src/zephyr/zephyr/ext/hal/nordic/. -isystem /home/carles/bin/zephyr-sdk/arm-zephyr-eabi/arm-zephyr-eabi/include -Os -imacros/home/carles/src/zephyr/zephyr/samples/hello_world/build/hello_world/nrf52840_pca10056/zephyr/include/generated/autoconf.h -ffreestanding -fno-common -g -mthumb -mcpu=cortex-m4 -imacros/home/carles/src/zephyr/zephyr/include/toolchain/zephyr_stdint.h -Wall -Wformat -Wformat-security -Wno-format-zero-length -Wno-main -Wno-pointer-sign -Wpointer-arith -Wno-unused-but-set-variable -Werror=implicit-int -fno-asynchronous-unwind-tables -fno-pie -fno-pic -fno-strict-overflow -fno-reorder-functions -fno-defer-pop -fmacro-prefix-map=/home/carles/src/zephyr/zephyr/samples/hello_world=CMAKE_SOURCE_DIR -fmacro-prefix-map=/home/carles/src/zephyr/zephyr=ZEPHYR_BASE -ffunction-sections -fdata-sections -mabi=aapcs -march=armv7e-m -std=c99 -MD -MT CMakeFiles/app.dir/src/main.c.obj -MF CMakeFiles/app.dir/src/main.c.obj.d -o CMakeFiles/app.dir/src/main.c.obj   -c ../../../src/main.c
../../../src/main.c:4:10: fatal error: array: No such file or directory
 #include <array>
          ^~~~~~~
compilation terminated.

@pabigot
Copy link
Collaborator

pabigot commented Jul 21, 2019

I'm unable to reproduce this with zephyr-1.14.0 and ZEPHYR_TOOLCHAIN_VARIANT=zephyr resolves to SDK 0.10.0. <array> is found and correct output is written. SDK 0.10.1 is unsupported in zephyr-1.14.0. It also works with current head of v1.14-branch.

I can confirm the problem as reported is present when ZEPHYR_TOOLCHAIN_VARIANT=gnuarmemb and current v1.14-branch.

@carlescufi it appears you're using a C source filemain.c instead of a C++ file main.cc, which would explain the failure to find <array>.

@carlescufi
Copy link
Member

I'm unable to reproduce this with zephyr-1.14.0 and ZEPHYR_TOOLCHAIN_VARIANT=zephyr resolves to SDK 0.10.0. <array> is found and correct output is written. SDK 0.10.1 is unsupported in zephyr-1.14.0. It also works with current head of v1.14-branch.

I can confirm the problem as reported is present when ZEPHYR_TOOLCHAIN_VARIANT=gnuarmemb and current v1.14-branch.

@carlescufi it appears you're using a C source filemain.c instead of a C++ file main.cc, which would explain the failure to find <array>.

Thanks @pabigot. I can confirm the same in master. There is clearly an issue with GNU Arm Embedded and newlib/C++. @galak or @SebastianBoe is that something you could take a look at?

@ioannisg ioannisg added the priority: medium Medium impact/importance bug label Jul 30, 2019
@ioannisg ioannisg added this to the v2.0.0 milestone Jul 30, 2019
@cszawisza
Copy link

I think that I've got the same error couple of months ago... this solved the issue

diff --git a/lib/libc/newlib/CMakeLists.txt b/lib/libc/newlib/CMakeLists.txt
index d368d1b89a8e1097eb7fda43044772c52738aba5..71061aa905131dbf85ead6dd0b56fb863e570b84 100644
--- a/lib/libc/newlib/CMakeLists.txt
+++ b/lib/libc/newlib/CMakeLists.txt
@@ -14,7 +14,7 @@ zephyr_library_sources(libc-hooks.c)
 # our libc headers to be picked instead of the toolchain's ffreestanding
 # headers.
 if(LIBC_INCLUDE_DIR)
-  zephyr_system_include_directories(${LIBC_INCLUDE_DIR})
+  zephyr_include_directories(${LIBC_INCLUDE_DIR})
 endif()
 
 if(LIBC_LIBRARY_DIR)

Can somebody confirm?

@pabigot
Copy link
Collaborator

pabigot commented Aug 12, 2019

I can confirm that the problem goes away by making newlib headers not system headers, but given 7d2df83 was just merged I'm not convinced it's the right solution. Presumably Zephyr and ARM do something different in builds that affects how system include files are handed.

@SebastianBoe
Copy link
Collaborator

SebastianBoe commented Aug 12, 2019

Hi, I have proposed a solution to the related issue #15603 here: #18189

The PR is intended to also fix this issue (17564), but I've not been able to reproduce all the reports here, so I'm not certain that it does so. Reports that show that #18189 does not fix #17564 for all environments would be welcome.

@simonmarti
Copy link
Author

simonmarti commented Aug 13, 2019

@cszawisza Thanks, the change fixes the problem for my environment.

@SebastianBoe I tested the change proposed in #18189 with my environment, but it does not solve the issue. The error message is still the same.

The environment I used for the test:

  • OS: Windows 10 1903
  • Toolchain: GNU ARM Embedded 7-2018-q2-update / 8-2019-q3-update
  • Zephyr: v2.0.0-rc1

@pabigot
Copy link
Collaborator

pabigot commented Aug 13, 2019

Based on this and other investigations I think a solution based on @cszawisza's patch is the right way forward, but it requires additional changes to reverse #15627 and re-solve that change's issue another way. @SebastianBoe I can take that on unless you want to handle it.

@SebastianBoe
Copy link
Collaborator

Hi,

please do take this off my hands, I've haven't made much progress.

@SebastianBoe SebastianBoe assigned pabigot and unassigned SebastianBoe Aug 13, 2019
pabigot added a commit to pabigot/zephyr that referenced this issue Aug 14, 2019
The solution from zephyrproject-rtos#14312 of using -isystem to prioritize the position of
the libc directory bypasses the effect of -ffreestanding with respect to
libc symbols expected to be present in a non-hosted environment.

Further, it breaks C++ with the ARM Embedded toolchain as the system
fails to find the right file with #include_next.

Use a more fine-grained solution that explicitly includes the underlying
newlib header required for <inttypes.h> support before moving on to
include the next available one, whether system or non-system.

Closes zephyrproject-rtos#17564

Signed-off-by: Peter Bigot <[email protected]>
galak pushed a commit that referenced this issue Aug 20, 2019
The solution from #14312 of using -isystem to prioritize the position of
the libc directory bypasses the effect of -ffreestanding with respect to
libc symbols expected to be present in a non-hosted environment.

Further, it breaks C++ with the ARM Embedded toolchain as the system
fails to find the right file with #include_next.

Use a more fine-grained solution that explicitly includes the underlying
newlib header required for <inttypes.h> support before moving on to
include the next available one, whether system or non-system.

Closes #17564

Signed-off-by: Peter Bigot <[email protected]>
pabigot added a commit to pabigot/zephyr that referenced this issue Aug 21, 2019
The solution from zephyrproject-rtos#14312 of using -isystem to prioritize the position of
the libc directory bypasses the effect of -ffreestanding with respect to
libc symbols expected to be present in a non-hosted environment.

Further, it breaks C++ with the ARM Embedded toolchain as the system
fails to find the right file with #include_next.

Use a more fine-grained solution that explicitly includes the underlying
newlib header required for <inttypes.h> support before moving on to
include the next available one, whether system or non-system.

Closes zephyrproject-rtos#17564

Backport: 96c1b05
Signed-off-by: Peter Bigot <[email protected]>
LeiW000 pushed a commit to LeiW000/zephyr that referenced this issue Sep 2, 2019
The solution from zephyrproject-rtos#14312 of using -isystem to prioritize the position of
the libc directory bypasses the effect of -ffreestanding with respect to
libc symbols expected to be present in a non-hosted environment.

Further, it breaks C++ with the ARM Embedded toolchain as the system
fails to find the right file with #include_next.

Use a more fine-grained solution that explicitly includes the underlying
newlib header required for <inttypes.h> support before moving on to
include the next available one, whether system or non-system.

Closes zephyrproject-rtos#17564

Signed-off-by: Peter Bigot <[email protected]>
nashif pushed a commit that referenced this issue Sep 25, 2019
The solution from #14312 of using -isystem to prioritize the position of
the libc directory bypasses the effect of -ffreestanding with respect to
libc symbols expected to be present in a non-hosted environment.

Further, it breaks C++ with the ARM Embedded toolchain as the system
fails to find the right file with #include_next.

Use a more fine-grained solution that explicitly includes the underlying
newlib header required for <inttypes.h> support before moving on to
include the next available one, whether system or non-system.

Closes #17564

Backport: 96c1b05
Signed-off-by: Peter Bigot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Build System area: C Library C Standard Library area: C++ bug The issue is a bug, or the PR is fixing a bug priority: medium Medium impact/importance bug
Projects
None yet
8 participants