In order to ensure that some initialization code runs before main
(using Arduino/avr-gcc) I have code such as the following:
class Init {
public
Here's a somewhat evil method of achieving this:
#include
static int bar = 0;
int __real_main(int argc, char **argv);
int __wrap_main(int argc, char **argv)
{
bar = 1;
return __real_main(argc, argv);
}
int main(int argc, char **argv)
{
printf("bar %d\n",bar);
return 0;
}
Add the following to the linker flags: --wrap main
eg.
gcc -Xlinker --wrap -Xlinker main a.c
The linker will replace all calls to main
with calls to __wrap_main
, see the ld man page on --wrap