-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Switch return type of main from 'void' to 'int' #54628
Conversation
As both C and C++ standards require applications running under an OS to return 'int', adapt that for Zephyr to align with those standard. This also eliminates errors when building with clang when not using -ffreestanding, and reduces the need for compiler flags to silence warnings for both clang and gcc Signed-off-by: Keith Packard <[email protected]>
As both C and C++ standards require applications running under an OS to return 'int', adapt that for Zephyr to align with those standard. This also eliminates errors when building with clang when not using -ffreestanding, and reduces the need for compiler flags to silence warnings for both clang and gcc. Most of these changes were automated using coccinelle with the following script: @@ @@ - void + int main(...) { ... - return; + return 0; ... } Approximately 40 files had to be edited by hand as coccinelle was unable to fix them. Signed-off-by: Keith Packard <[email protected]>
These changes were automated using coccinelle with the following script: @@ @@ - void + int main(...) { ... - return; + return 0; ... } Signed-off-by: Keith Packard <[email protected]>
Looks like switching the main return value to int means that stack frame persists and increases stack usage by a few bytes. Increase the main stack size to avoid overflows. Signed-off-by: Keith Packard <[email protected]>
These flags were added to avoid warnings when main was declared to return void. Now that main returns int, those warnings will flag errors. Signed-off-by: Keith Packard <[email protected]>
Remove RETURN_FROM_MAIN macro which performed control flow, violating compliance tests. Replace with explicit use of posix_exit_main or return as appropriate. Signed-off-by: Keith Packard <[email protected]>
Add a paragraph explaining the change from void main(void) to int main(void) and the requirement for main functions to return zero. Signed-off-by: Keith Packard <[email protected]>
This applies the coccinelle script to another set of files: samples/bluetooth/bthome_sensor_template/src/main.c samples/boards/stm32/power_mgmt/standby_shutdown/src/main.c samples/drivers/smbus/src/main.c samples/drivers/virtualization/ivshmem/doorbell/src/ivshmem.c samples/fuel_gauge/max17048/src/main.c samples/hello_world/src/main.c samples/sensor/proximity_polling/src/main.c samples/subsys/logging/ble_backend/src/main.c tests/drivers/build_all/mfd/src/main.c Signed-off-by: Keith Packard <[email protected]>
rebased, repaired and re-pushed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will need to be removed but it can be done in a later commit, no point holding this up anymore:
config CPP_MAIN
bool "C++ main() function definition"
help
This option instructs the Zephyr kernel to call the 'int main(void)'
instead of the 'void main(void)', which is the default main() type
for Zephyr.
C++ does not allow the main() to be defined with 'void' return type,
and any applications defining its main() in a C++ source file must
enable this option.
CC @stephanosio
And ./arch/posix/include/posix_cheats.h too
Yes, I will open a follow-up PR. |
I just realized that we can add |
Since zephyrproject-rtos/zephyr#54628, Zephyr's main() returns int. Signed-off-by: Yasushi SHOJI <[email protected]>
As both C and C++ standards require applications running under an OS to
return 'int', adapt that for Zephyr to align with those standard. This also
eliminates errors when building with clang when not using -ffreestanding,
and reduces the need for compiler flags to silence warnings for both clang
and gcc.
Related discussion: #54336 (comment)