0

I want to change main so that it calls one function before doing anything else. So I written code like this

#include <stdio.h>

void test()
{
    printf("\nTEST\n");
#undef main
    main(1,NULL);
}

int main(int argc, char** argv)
{
    printf("\nHello World\n");
}

and compiled it like

cc -g -Dmain=test test.c -o test

but still it prints "Hello World" and not "TEST". What should I do so that I can call test just before main does anything else?

Thanks

user3494614
  • 603
  • 1
  • 7
  • 20
  • 1
    Why not just call the function first thing you do in `main`? – Art May 07 '14 at 11:27
  • 1
    http://stackoverflow.com/questions/7494244/how-to-change-the-entry-point-in-gcc tells you how to change the entry point with gcc – Ferenc Deak May 07 '14 at 11:34

1 Answers1

3

If you want call other function, before main, gcc provides __attribute__

for example:

int test(void) __attribute__ ((constructor));

int test()
{
    printf("\nTEST\n");
    return 0;
}

int main(int argc, char** argv)
{
    printf("\nHello World\n");
    return 0;
}