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

Switch return type of main from 'void' to 'int' #54628

Merged
merged 9 commits into from
Apr 13, 2023

Conversation

keith-packard
Copy link
Collaborator

@keith-packard keith-packard commented Feb 8, 2023

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)

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]>
@keith-packard
Copy link
Collaborator Author

@keith-packard there are no TSC objections to merging this. Could you rebase and address the missing ones that @nordicjm found? Thanks!

rebased, repaired and re-pushed.

Copy link
Collaborator

@nordicjm nordicjm left a 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

@stephanosio
Copy link
Member

This will need to be removed but it can be done in a later commit, no point holding this up anymore:

Yes, I will open a follow-up PR.

@keith-packard
Copy link
Collaborator Author

I just realized that we can add -Wmain to the compiler flags if we want to catch applications using the wrong type for main. This works with gcc even with -ffreestanding, but doesn't appear to work with clang with that flag.

@stephanosio stephanosio merged commit 1d5e644 into zephyrproject-rtos:main Apr 13, 2023
@keith-packard keith-packard deleted the int-main branch April 13, 2023 22:57
yashi added a commit to yashi/st-l6470 that referenced this pull request Apr 22, 2024
Since zephyrproject-rtos/zephyr#54628, Zephyr's main() returns int.

Signed-off-by: Yasushi SHOJI <[email protected]>
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 area: C++ area: Portability Standard compliant code, toolchain abstraction Release Notes Required Release notes required for this change treewide 🧹
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Switch return type of main to 'int'