Why i can overrid the main function?

Why i can overrid the main function? I am not find ‘‘attribute((weak))’’ in main.c

I don’t know what you mean, but why would you want to?

The essence of the Arduino system is that you don’t need to fiddle with the main() function - just work with setup() and loop() and any other functions that you will create.

…R

csensors:
Why i can override the main function? I am not find ''attribute((weak))'' in main.c

Good question. I suspect that the build process doesn't include main.cpp from the Arduino core when it sees that you have main() declared in your sketch.

The linker considers modules pulled in from a library "weak" in comparison to modules explicitly specified for inclusion.

Armed with the fact above and a verbose output from a build you should now be able to answer the question for yourself.

In here User overridable `setup`, `loop` and `main` through `__attribute__ ((weak))` by cipriancraciun · Pull Request #1139 · arduino/Arduino · GitHub the cipriancraciun offer a patch:

#include <Arduino.h>

extern "C" void setup(void) __attribute__((weak));
extern "C" void loop(void) __attribute__((weak));

__attribute__((weak))
int main(void)
{
	init();

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

But in fact the Arduino did not accept the patch.I want to know how to achieve the “weak” main function in the end. Looking forward to your reply.

Robin2:
I don't know what you mean, but why would you want to?

The essence of the Arduino system is that you don't need to fiddle with the main() function - just work with setup() and loop() and any other functions that you will create.

...R

In some lib source code,I see they override the main(),but i just want to know why this can be done?

csensors:
In here User overridable `setup`, `loop` and `main` through `__attribute__ ((weak))` by cipriancraciun · Pull Request #1139 · arduino/Arduino · GitHub the cipriancraciun offer a patch:

Which is unrelated to your question.

I want to know how to achieve the "weak" main function in the end.

It is already weak by the way the Arduino IDE builds the image.

Looking forward to your reply.

You've already seen it.

Ok,thank you! But could you show me the Arduino IDE code where about the “weak”?

csensors:
Ok,thank you! But could you show me the Arduino IDE code where about the "weak"?

There is nothing to show you beyond what is already in this thread. main is weak because of the way the linker works and because of the physical location of main when the linker is invoked.

Thanks!

The Arduino platform comes with a main() function that supplies several conveniences that makes life easier for new programmers. It does hardware initialization, it calls setup and loop, and I belive it polls the USB for sketches being uploaded.

You can override this with your own main because - well, maybe you don't want to do things that way. Maybe you want to customise the hardware setup. Maybe you want to create a multitasking kernel that does not use loop(), Without the weak attribute on the default implementation if main(), doing this would be difficult - you'd have to recompile/rewrite parts of the underlying platform code.

PaulMurrayCbr:
... Without the weak attribute on the default implementation if main()...

main is not tagged with weak.