Question

I saw redefine function here using macro in c. So I am interesting is it possible to redefine main function?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

int _main(int argc, char **argv)
{
    printf("New Original main function\n");
    return main(argc, argv);
}

#ifdef DEBUG
#define main(argc, argv) _main(argc, argv)
#endif

Code compiled with out any problem but I am getting:

Original main function

So I am wondering why it does not work? When I use same techniques for malloc and free functions it works perfect. So what is wrong?

Why I want to do something like this? I want to do some code before main function will be executed. Is it possible in this way? if not is there are some other way?

P.S.: Sorry I did not mention in question. I am using gcc in Ubuntu OS. If you are down voting please give a reason in comments. You comments is very useful to my further development.

Was it helpful?

Solution 2

If your question is really: "can i execute code before main?" Then the answer is an emphatic YES.

Since you are using GCC, you can use function attributes (http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html) to mark a function as a constructor.

void pre_main_function (void) __attribute__ ((constructor));

A useful example can be found at http://www.geeksforgeeks.org/functions-that-are-executed-before-and-after-main-in-c/

EDIT

The following syntax can also be used:

__attribute__ (( constructor(n) ))

where n specifies the priority, allowing you to mark multiple functions to be executed before main whilst giving you control over the execution order ( the lower the value of n, the earlier the function is executed.

OTHER TIPS

If you want to change entry point of your program, you don't need play with defines. You can use linker's -e option for that:

gcc -Wl,-e,__main ...

Please note extra underscore. Depending on some options, the symbol name can be different.

Your #define does not change the main function at all - it is a macro preprocessor.

The only effect of your #define will be to change the call to main in _main into a recursive call to _main(). But since _main is not called, this is dead code. This is what your code looks like after the preprocessor has run...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

int _main(int argc, char **argv)
{
    printf("New Original main function\n");
    return _main(argc, argv); /* recursive call due to macro replace */
}

This then leads to the next question - which is why redefine main at all? If you want some entirely different code to run on debug simply declare main as

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
#ifdef DEBUG
    return debugApp( argc, argv);
#else
    return productionApp( argc, argv);
#endif
}

N.B Just because you can do something doesn't mean you should do it. :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top