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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
12-11-2008, 08:06 PM
|
#1
|
LQ Newbie
Registered: Dec 2008
Location: Bangalore,India
Posts: 10
Rep:
|
C programming in Linux
who calls main function in c programming?
|
|
|
12-12-2008, 01:34 AM
|
#2
|
Member
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 657
Rep:
|
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.
|
|
|
12-12-2008, 10:07 AM
|
#3
|
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286
|
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.
|
|
|
12-12-2008, 11:17 AM
|
#4
|
Senior Member
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Rep:
|
Quote:
Originally Posted by sandeep_raju123
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.
|
|
|
12-12-2008, 01:40 PM
|
#5
|
ELF Statifier author
Registered: Oct 2007
Posts: 676
Rep:
|
Quote:
Originally Posted by ErV
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.
|
|
|
12-12-2008, 01:46 PM
|
#6
|
Senior Member
Registered: Sep 2004
Location: Nashville
Distribution: Manjaro, RHEL, CentOS
Posts: 2,098
Rep:
|
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.
|
|
|
All times are GMT -5. The time now is 04:42 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|