From 5a07588ae485e23366af05f218a023457986e6dc Mon Sep 17 00:00:00 2001 From: Alberto Escolar Piedras Date: Tue, 13 Jun 2023 15:58:39 +0200 Subject: [PATCH] kernel: Explicitly define the main prototype if C++ If the embedded application has a main(), Zephyr requires it to have a signature with C linkage (no C++ name mangling). Otherwise Zephyr's init will not call it, but will call the default weak stub. But, when building with clang/llvm, when we build calling the compiler with --frestanding (for ex if CONFIG_MINIMAL_LIBC is set), the compiler will mangle the main() symbol name. This is not incorrect behavior from the compiler, as, in principle, in a freestanding environment main() does not have a special meaning. gcc is still not mangling it when called with --frestanding. To avoid this issue, we define explicitly the main linkage as extern C, in a header the application will always include. Signed-off-by: Alberto Escolar Piedras --- include/zephyr/types.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/zephyr/types.h b/include/zephyr/types.h index 80e80d4de74..7e5e87b2ce3 100644 --- a/include/zephyr/types.h +++ b/include/zephyr/types.h @@ -29,6 +29,11 @@ typedef union { void (*thepfunc)(void); } z_max_align_t; +#ifdef __cplusplus +/* Zephyr requires an int main(void) signature with C linkage for the application main if present */ +extern int main(void); +#endif + #ifdef __cplusplus } #endif