LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-25-2004, 11:56 PM   #1
joeyBig
LQ Newbie
 
Registered: Jul 2004
Posts: 23

Rep: Reputation: 15
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.
 
Old 11-26-2004, 12:20 AM   #2
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
I don't think you can. However, you can workaround in this fashion

Code:
// ...
// ...
int mymain ()
{
       // your program here
       //...
       //...
       return code;
}

int main ()
{
       return ( mymain () );
}
 
Old 11-26-2004, 12:35 AM   #3
joeyBig
LQ Newbie
 
Registered: Jul 2004
Posts: 23

Original Poster
Rep: Reputation: 15
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.
 
Old 11-26-2004, 01:14 AM   #4
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
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?
 
Old 11-26-2004, 01:45 AM   #5
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
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?
 
Old 11-26-2004, 02:02 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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
pain()
{
        printf("ouch\n");
}
$ gcc -c a.c
$ /usr/bin/ld -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 \
/usr/lib/crti.o crtz.o \
/usr/lib/gcc-lib/i586-mandrake-linux-gnu/2.96/crtbegin.o \
-L/usr/lib/gcc-lib/i586-mandrake-linux-gnu/2.96 \
-L/usr/lib/ a.o -lgcc -lc -lgcc \
/usr/lib/gcc-lib/i586-mandrake-linux-gnu/2.96/crtend.o  \
/usr/lib/crtn.o -o a
$ ./a
ouch
$ gdb a
GNU gdb 5.1.1
...
(gdb) b main
Function "main" not defined.
(gdb) b pain
Breakpoint 1 at 0x8048406
 
Old 03-19-2018, 01:12 AM   #7
Anshu Prince
LQ Newbie
 
Registered: Mar 2018
Posts: 1

Rep: Reputation: Disabled
Talking 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
 
Old 03-19-2018, 04:19 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
It this a nonstandard compiler-extension, or a Microsoft-specific feature?
 
Old 03-19-2018, 06:37 AM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How many prototype of function main in c? ypzhuang Programming 9 12-13-2008 07:53 AM
what's the kernel's main() function? LordOfer Linux - General 1 04-06-2005 03:02 PM
Array declaration in class or main function??? redhatrosh Programming 4 03-15-2005 02:13 PM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
in whst file is the main () function for the kernel? wsimmons Programming 2 12-30-2001 12:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration