Skip to content
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

libc: Fix include order #84943

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

yperess
Copy link
Collaborator

@yperess yperess commented Jan 30, 2025

I ran into an issue with the include order when building Zephyr using Bazel. This seems to check out when I walked through the include order, there's a circular dependency here that's resolved by moving the definition up.

<zephyr/sys/errno_private.h> depends on errno being defined. With strict
compiler warnings enabled, this causes a build failure.

Signed-off-by: Yuval Peress <[email protected]>
@@ -28,14 +28,15 @@
* @{
*/

/* errno_private.h depends on errno being defined first. */
#define errno (*z_errno())

Copy link
Collaborator

Choose a reason for hiding this comment

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

This doesn't make sense to me -- the only place errno_private.h uses errno directly is in defining z_errno, so this define would result in a circular definition?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Let me see if I can retrace this (it took me a long time to get Zephyr to build with Bazel so this change was pretty old.

  • lib/libc/minimal/include/errno.h includes zephyr/sys/errno_private.h
  • zephyr/sys/errno_private.h with CONFIG_LIBC_ERRNO includes errno.h which is a no-op since we have already #define ZEPHYR_LIB_LIBC_MINIMAL_INCLUDE_ERRNO_H_
  • Then zephyr/sys/errno_private.h defines z_errno() which returns &errno but errno is only defined after zephyr/sys/errno_private.h is included.

Copy link
Member

Choose a reason for hiding this comment

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

I'm curious to know why Zephyr hasn't encountered this error before.

Copy link
Collaborator

Choose a reason for hiding this comment

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

But we don't want errno defined as (*z_errno()) when we define z_errno as that's just an infinite loop. If you want to use CONFIG_LIBC_ERRNO, you need the minimal C library to have its own definition of errno, presumably as a global variable. Probably something like this:

#ifdef CONFIG_LIBC_ERRNO
int errno;
#endif
#include <zephyr/sys/errno_private.h>
#define errno (*z_errno())

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, let me rebase and try without this commit. I'll report back tonight.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: C Library C Standard Library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants