LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-11-2008, 08:06 PM   #1
sandeep_raju123
LQ Newbie
 
Registered: Dec 2008
Location: Bangalore,India
Posts: 10

Rep: Reputation: 0
Smile C programming in Linux


who calls main function in c programming?
 
Old 12-12-2008, 01:34 AM   #2
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 657

Rep: Reputation: 40
That depends on who calls it. When you start a c program from the shell, I'd believe that it the shell that makes the call.
 
Old 12-12-2008, 10:07 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
There is some startup module linked into each image. I'm not certain of the details, but roughly:

Some loader code executes first inside the image, loading required .so files and doing some relocations. It transfers control to the startup module (as a jump, not a call).

The startup module does some initialization (a lot of things in C++, I'm not sure how much in C) and pushes the arguments for main and calls main. When main returns, it does a few kinds of cleanup before exiting to the OS.

In many compile/link packages, that startup module is named crtbegin.
 
Old 12-12-2008, 11:17 AM   #4
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by sandeep_raju123 View Post
who calls main function in c programming?
Looks like homework to me. Oh well.

AFAIK, C runtime library calls it from real entry point - after it builds argument lists (& etc) using os-specific calls. I'd recommend you to check gcc sources to find out how exactly things work.
 
Old 12-12-2008, 01:40 PM   #5
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 676

Rep: Reputation: 137Reputation: 137
Quote:
Originally Posted by ErV View Post
Looks like homework to me. Oh well.

AFAIK, C runtime library calls it from real entry point - after it builds argument lists (& etc) using os-specific calls. I'd recommend you to check gcc sources to find out how exactly things work.
It's not in the gcc source, but in the glibc - code for ld-linux.so.
 
Old 12-12-2008, 01:46 PM   #6
jstephens84
Senior Member
 
Registered: Sep 2004
Location: Nashville
Distribution: Manjaro, RHEL, CentOS
Posts: 2,098

Rep: Reputation: 102Reputation: 102
From what I understand the program is the one that calls main in an indirect way. First when the binary is called a jump is made to an entry point but that point is not main() but a runtime library which will first activate a few things stdin/stdout/stderr and then some other things then call main().

Try searching google for c + application + "entry point"

Last edited by jstephens84; 12-12-2008 at 01:48 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:42 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