Skip to content

cmake: Don't use -nostdinc with CXX #18189

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

Conversation

SebastianBoe
Copy link
Collaborator

@SebastianBoe SebastianBoe commented Aug 12, 2019

It is observed that the C++ standard library headers can not be found
when -nostdinc is used. To fix this we omit -nostdinc when building
for C++.

To reproduce the issue use the latest zephyr master and add

#include <vector>

to the list of includes in samples/cpp_synchronization.

Without this fix the sample will not build for for gnuarmemb or the
Zephyr SDK.

This fixes #15603 and uses the fix suggested here: #15603 (comment)

It is observed that the C++ standard library headers can not be found
when -nostdinc is used. To fix this we omit -nostdinc when building
for C++.

To reproduce the issue use the latest zephyr master and add

 #include <vector>

to the list of includes in samples/cpp_synchronization.

Without this fix the sample will not build for for gnuarmemb or the
Zephyr SDK.

Signed-off-by: Sebastian Bøe <[email protected]>
@SebastianBoe SebastianBoe requested a review from nashif as a code owner August 12, 2019 12:19
@ioannisg ioannisg added the bug The issue is a bug, or the PR is fixing a bug label Aug 12, 2019
@ioannisg ioannisg requested a review from carlescufi August 12, 2019 12:47
@carlescufi carlescufi requested a review from pabigot August 12, 2019 12:47
@galak
Copy link
Collaborator

galak commented Aug 12, 2019

If we do this for C++ why wouldn't we do it for C?

Copy link
Collaborator

@pabigot pabigot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally when using C++ you need CONFIG_NEWLIB_LIBC=y to access the C++ standard headers. In that case this branch is already bypassed, so the fix doesn't work.

@SebastianBoe
Copy link
Collaborator Author

SebastianBoe commented Aug 12, 2019

@pabigot : Would you kindly post steps to reproduce (where this fix doesn't work) with the latest Zephyr master and SDK/gnuarmemb versions?

@SebastianBoe
Copy link
Collaborator Author

If we do this for C++ why wouldn't we do it for C?

Using -nostdinc is inherited from the Linux kernel:
https://github.com/torvalds/linux/blob/8e61ea11c23b108c8ce062f791dfa737896da346/scripts/Makefile.lib#L162

Why the linux kernel does this I do not know. IIRC I attempted to get rid of it at some point but was unable to.

That being said, if there are some unwanted side-effects of dropping -nostdinc then I would rather find out in the C++ build than have it crop up in the more mainstream C build.

@pabigot
Copy link
Collaborator

pabigot commented Aug 12, 2019

Do exactly the case that works with this patch, but instead of #include <vector> use #include <array>.

Using this PR and gcc-arm-none-eabi-8-2019-q3-update the build fails even without adding CONFIG_NEWLIB_LIBC=y, though the cascade of errors is different. With CONFIG_NEWLIB_LIBC=y it fails to find the next stdlib.h as it does without this patch.

I've done some diagnostic builds and there's no difference in compiler/preprocessor invocation between the Zephyr toolchain and the ARM one except the path to the system include directory. There's no obvious difference in the contents of the system include directory, ignoring newlib version 3.1 vs 3.0. There must be a difference in the way gcc and newlib are configured in the Zephyr SDK that changes the behavior.

My setup is:

export ZEPHYR_TOOLCHAIN_VARIANT=gnuarmemb
export GNUARMEMB_TOOLCHAIN_PATH=/usr/local/gcc-arm-none-eabi
cd /mnt/devel/external/zp/zephyr
. zephyr-env.sh

@pabigot
Copy link
Collaborator

pabigot commented Aug 12, 2019

I don't see --enable-languages=c,c++ in the SDK builds, which might explain it.

Nor --with-newlib, which could also explain it. There are actually a lot of differences in Zephyr SDK ARM builds from the ARM Embedded build script, including probably some patches to GCC and newlib.

Copy link
Collaborator

@galak galak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a testcase as part of this for using stdlib C++ functionality. So that way we know if this breaks in the future.

Something like:

void test_main(void)
{
	/* Does nothing.  This is a compile only test. */
	vector v;
	std::vector<int> v;

	v.push_back(10);
}

@SebastianBoe
Copy link
Collaborator Author

Closing as this was not the correct approach.

@SebastianBoe SebastianBoe deleted the avoid_nostdinc_on_cxx branch August 16, 2019 12:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Build System bug The issue is a bug, or the PR is fixing a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to use C++ Standard Library
6 participants