Standard C program structure

Dunno if this is a dumb idea or nor. I recently decided I wanted to reformat a program written Arduino style with a “void setup()” and a “void loop()” to a more standard C program structure with a “int main()” with a “while (true)” loop at the end of it. However the program no longer worked. It compiled fine, but nothing was executed. It seems the setup() and loop() constructs have to be there for the program to work.
Just wondering if it’s possible to convert an Arduino style structure to a standard C structure, and if so, how?
All advice gratefully received.
Thanks.

This is the Arduino main function of the AVR core (ATmega…) (see GitHub):

int main(void)
{
	init();

	initVariant();

#if defined(USBCON)
	USBDevice.attach();
#endif
	
	setup();
    
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}
        
	return 0;
}

If you replace it with your own, several thing will be missing, e.g. the calls to init() and initVariant().

Furthermore, your code is no longer portable to other platforms (e.g. ESP32, STM32) as the main function looks different on each platform.

2 Likes

@manuelbl thanks for the explanation. I’m a bit confused though. If I set up the program with my own “int main()” function does it overwrite the default PlatformIO one? My program successfully compiled with the new format, and I’m assuming that the presence of two functions called “main” would generate a compile error, would it not? And wouldn’t the absence of the setup() and loop() functions also generate compile errors?

Maybe a bit of background would be in order. I am (experimentally) developing an application using the Zerynth ide, which allows microcontrollers to be programmed with (a subset of) Python. Unfortunately Zerynth’s support for some hardware features is a bit thin, especially timers and pwm, however it does have a slick method for calling C programs from within the Python program. So I have developed a C program (for an Arduino Due) that generates the pwm functionality I want using hardware registers, with Platformio, and now I’d like to call that program from within my Python program. However, to do that Zerynth has to compile the C program and do the appropriate linking. Needless to say it doesn’t have access to the Arduino/Platformio libraries, so the compile comprehensively fails. Removing the setup() and loop() keywords from the program was my first tentative foray into making the program more compatible. Although, if I think about it, if I did discover a way to plug the required libraries into Zerynth then the Arduino format would be more likely to work anyway. But that’s a question for the Zerynth forum, not here.
:wink:

Regarding the expected linker error: the Arduino core main function resides in a library. So the linker will only pull it in if needed. But since it already has your main function, it won’t do it and won’t complain either. And since it Arudino main is not used, there’s no need for setup and loop either.

@manuelbl thanks for the reply. I suspected it might be something like that. I did some more experimenting, just for fun, with a blinky program. I found that the program would compile with a “main()” function, but didn’t work, the led didn’t flash. I tried adding the “init()” and “initVariant()” functions to see if that made any difference, and then it wouldn’t compile, and did complain about multiple functions called “main()”.
Anyway, doesn’t really matter, just interesting.
:wink: