0

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.

Community
  • 1
  • 1
Khamidulla
  • 2,927
  • 6
  • 35
  • 59
  • I don't really undertand what you want to achieve here. If you want to perform code before main, what is the functional difference in doing this as a first step in main and then start the main program? If you really want to perform code before main, you must modify the crt0 startup code. In C++ you could achieve that by using static variables. which are intiliazed before main is called. In C this is not possible. – Devolus Mar 04 '14 at 11:22
  • see this question for how main works http://stackoverflow.com/q/19419569/2549281 – Dabo Mar 04 '14 at 11:23
  • Are you looking for this:- http://gcc.gnu.org/onlinedocs/gcc/Executing-code-before-main.html? – Rahul Tripathi Mar 04 '14 at 11:24
  • @Devolus I want to register callback function using atexit() before main function. – Khamidulla Mar 04 '14 at 11:24
  • @remyabel I am using gcc not g++. I am using c not c++. – Khamidulla Mar 04 '14 at 11:25
  • @downvoter please give a reason I can improve my question quality. – Khamidulla Mar 04 '14 at 11:26
  • @Phoenix: so why do you want to register a callback function **before** the main function ? Just call `atexit` at the beginning of `main`. – Jabberwocky Mar 04 '14 at 11:36
  • @MichaelWalz Thank you for your comment. I am learning possibility. And want to know Is it possible or not? If it is, How it can be achieved. – Khamidulla Mar 04 '14 at 11:39
  • AFAIK: you cannot execute anything **before** main. Anyway this is not needed, because doing something **before** main is actually the same thggn as doing it at the **beginning** of main. – Jabberwocky Mar 04 '14 at 11:41
  • @MichaelWalz Thank you. But one more thing. I thought macro will be executed before main function. – Khamidulla Mar 04 '14 at 11:55
  • 4
    Macros are not executed, they are pre-processed. Basically, they are just text replacement done by the compiler when you compile your program. – Lundin Mar 04 '14 at 12:16

3 Answers3

2

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.

Valeri Atamaniouk
  • 5,125
  • 2
  • 16
  • 18
  • Your approach works also but I going to use @mjs answer. I gave you vote up yesterday. Sorry that I cannot accept two answer. Thank you for your effort. – Khamidulla Mar 05 '14 at 02:48
1

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. :-)

vogomatix
  • 4,856
  • 2
  • 23
  • 46
1

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.

mjs
  • 2,837
  • 4
  • 28
  • 48
  • It does what I want. Thank you once more. – Khamidulla Mar 05 '14 at 02:47
  • This is a feature of the GCC compiler, not a feature of the C language, right? – Mike Sherrill 'Cat Recall' Mar 05 '14 at 11:14
  • In all honesty, I don't know if this is a GCC extension or part of the standard (and pick one of those...). The GCC doc says "In GNU C....", which implies to me that it is a GCC extension. Having said that, other compilers also support this concept - eg WindRiver Compiler (aka diab). And the user guide for that includes this in the section on Additions to ANSI C and C++. So i think its a compiler feature. – mjs Mar 05 '14 at 11:31