zephyr/kernel/main_weak.c
Stephanos Ioannidis fa5fd41b61 kernel: Add C++ main() support
The C++ standard requires the main() function to have the return type
of 'int' and does not allow the main() to be defined with the 'void'
return type. Moreover, GCC goes as far as to emit a hard error when the
'::main()' has the return type of `void`.

This commit introduces an option to instruct the Zephyr kernel to call
the 'int main(void)' instead of the 'void main(void)' in case a Zephyr
application defines main() in a C++ source file.

Signed-off-by: Stephanos Ioannidis <stephanos.ioannidis@nordicsemi.no>
2022-11-05 16:41:45 +09:00

37 lines
1 KiB
C

/*
* Copyright (c) 2010-2014 Wind River Systems, Inc.
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
/* Linkers may treat weak functions differently if they are located within
* the same object that calls the symbol or not.
*
* For example, when using armlink, then if the weak symbol is inside the object
* referring to it the weak symbol will be used. This will result in the symbol
* being multiply defined because both the weak and strong symbols are used.
*
* To GNU ld, it doesn't matter if the weak symbol is placed in the same object
* which uses the weak symbol. GNU ld will always link to the strong version.
*
* Having the weak main symbol in an independent file ensures that it will be
* correctly treated by multiple linkers.
*/
#include <kernel_internal.h>
#ifdef CONFIG_CPP_MAIN
int __weak main(void)
#else
void __weak main(void)
#endif
{
/* NOP default main() if the application does not provide one. */
arch_nop();
#ifdef CONFIG_CPP_MAIN
return 0;
#endif
}