How to Override main function in C
Hi
main() is the important function without which a C pgm cannot be run. I need to override the basic functionality of main() and use my own function say, mymain() in replacement of main() how to perform this. thanks and regards, joe. |
I don't think you can. However, you can workaround in this fashion
Code:
// ... |
hi shankar,
thanks for u'r reply, but overriding main function can be done for sure. I need to program in this format... //... joemain( ) { /* Here main should be replaced by my own function joemain */ //..... functioncall ( ); //...... return val; } //..... functioncall( ) { //.... } where in, if main() function is called, compiler may return 'main function not found / defined'. (I am not sure of the above result, but sure that main can be overriden) I suppose you understood. regards, joe. |
Still, I don't believe you can do that. I'm not a gcc expert, but the embedded systems compilers I've used probably work in a similar manner to gcc. There our linker combined a small assembly file start.a with the program being linked, and it implicitly linked this start.a code in first, before your .o files, so its entry point was exactly at the top of the code segment. The start.a code did things like initialize the program stack, and then finally it jumped to main(). The linker resolved main because that symbol was object of the final jmp in the start.a file. I don't see how you can change that without changing the internals of the linker. What's to gain by doing this again?
|
This is not possible because the underlying linker (as randyding mentioned) will not resolve main () so you'll get a linker error while building your project. What's the big deal anyway to change main to joemain? It doesn't enhance your code in any way, does it?
|
Well, it can be done, but is obviously useless ...
Anyway, I liked the challenge, so here's how I did it: I first build my own crt1.o file where the main symbol has been replaced by the new one. I just patched crt1.o, replaced "main" by "pain" in its symbol table, and saved the new version in crtz.o. Then I linked my object file with this crtz.o instead of the original crt1.o, that's it. Code:
$ cat a.c |
using #pragma directive
#include<stdio.h>
#include<conio.h> void School(); void College() ; #pragma startup School 105 #pragma startup College #pragma exit College #pragma exit School 105 void main(){ printf("\nI am in main"); getch(); } void School(){ printf("\nI am in School"); getch(); } void College(){ printf("\nI am in College"); getch(); } Output : I am in College I am in School I am in main I am in School I am in College |
It this a nonstandard compiler-extension, or a Microsoft-specific feature?
|
Hi Some things to check Anshu Prince,
If you click on a user's profile, such as joeyBig, you get a pulldown menu where you can see their profile. In the profiles it shows the last activity by that user. In this case joeyBig was last seen logged into LQ back in 2004. And also were the most recent replies to this thread. However, fine answer for the question. :) |
All times are GMT -5. The time now is 04:56 AM. |