Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Program without main

Reply
Thread Tools

Program without main

 
 
Guruz
Guest
Posts: n/a
 
      05-14-2004
hi C gurus do anyone of u know how to write a program in C without
main and still create a executable out of it. Remember, I said no
main() function not in
-->include files
-->libraries
-->no define thisthat main

I mean absolutely no main() atall

Well to start off here one method :
main() is called in C run time object crt0.o. But if we want to write
crt0.S of our own what r the things we have to do? can anyone explain
me whats going on behind this C run time?
 
Reply With Quote
 
 
 
 
Darrell Grainger
Guest
Posts: n/a
 
      05-14-2004
On Fri, 14 May 2004, Guruz wrote:

> hi C gurus do anyone of u know how to write a program in C without
> main and still create a executable out of it. Remember, I said no
> main() function not in
> -->include files
> -->libraries
> -->no define thisthat main
>
> I mean absolutely no main() atall
>
> Well to start off here one method :
> main() is called in C run time object crt0.o. But if we want to write
> crt0.S of our own what r the things we have to do? can anyone explain
> me whats going on behind this C run time?


There is no crt0.o on my system. The fact that your compiler calls an
entry point in crt0.o that in turn calls main is incredibly compiler
specific.

It is not uncommon to have some entry point that initializes global
variables, sets up the stack, etc. and then calls the main function. It is
often possible to write your own start up routine that does not call main.

BUT it is not guaranteed that there exists code before main. All the setup
and initialization could occur by the operating system's loader and your
program execution actually starts at main. In that case you MUST have a
main.

Therefore, whatever solution someone comes up with it will off-topic for
comp.lang.c. It will only apply to a specific implementation of a C
compiler.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      05-14-2004
Guruz wrote:
> hi C gurus do anyone of u know how to write a program in C without
> main and still create a executable out of it. Remember, I said no
> main() function not in
> -->include files
> -->libraries
> -->no define thisthat main
>
> I mean absolutely no main() atall


You should invest some time in learning to spell the English
language correctly. "u" is a letter. "atall" should be "at all".

Yes, many people know how to write a program in C without a main()
function. I've done it myself on many embedded systems. On my
current project, we don't define "main" so that we can use our
own libraries and start-up code.


> Well to start off here one method :
> main() is called in C run time object crt0.o.


Perhaps with your compiler, but not with mine.


> But if we want to write
> crt0.S of our own what r the things we have to do?


Depends greatly on "ur" implementation (compiler). Some compilers
allow you to bypass their run-time library and start up code and
provide "u" w/ the documentation on how "2" do it.


> can anyone explain
> me whats going on behind this C run time?


Yes, many people can.
In a few words:
There is a bit of code that is executed before the "main" function.
This code initializes the Run-Time library and variables.
Once this is set up the main() function (or "ur" function) can
then be executed. From this point, execution follows your program.
Some implementations also have code which is executed upon the
end or termination of "ur" program.

For more information, research these topics:
Compiler Design & Theory
Operating Systems
"ur" implementation (compiler, linker, etc.)

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      05-14-2004
In <(E-Mail Removed) > (E-Mail Removed) (Guruz) writes:

>hi C gurus do anyone of u know how to write a program in C without
>main and still create a executable out of it. Remember, I said no
>main() function not in
>-->include files
>-->libraries
>-->no define thisthat main
>
>I mean absolutely no main() atall


Such a program would not be a hosted C program *by definition*.

>Well to start off here one method :
>main() is called in C run time object crt0.o. But if we want to write
>crt0.S of our own what r the things we have to do? can anyone explain
>me whats going on behind this C run time?


Nope, because the C language definition is completely silent on this
issue. The purpose of crt0.o is to interface the code generated by your
compiler to the OS that is executing it. Therefore, its code is specific
to both the OS and the C compiler using it. Your only hope is to find
the source code of your startup module and to study it.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      05-14-2004
(E-Mail Removed) (Guruz) writes:

> hi C gurus do anyone of u know how to write a program in C without
> main and still create a executable out of it.


Perhaps you should look into freestanding implementations.
--
"Give me a couple of years and a large research grant,
and I'll give you a receipt." --Richard Heathfield
 
Reply With Quote
 
Mitchell
Guest
Posts: n/a
 
      05-15-2004
On Fri, 14 May 2004 15:25:05 GMT, Thomas Matthews
<(E-Mail Removed)> wrote:

>Guruz wrote:
>> hi C gurus do anyone of u know how to write a program in C without
>> main and still create a executable out of it. Remember, I said no
>> main() function not in
>> -->include files
>> -->libraries
>> -->no define thisthat main
>>
>> I mean absolutely no main() atall

>
>You should invest some time in learning to spell the English
>language correctly. "u" is a letter. "atall" should be "at all".


And it should be "does anyone" not "do anyone".

Yes. In VC++ I see a linker option /ENTRY:function to specify entry
point function for an executable. However it will bypass calling
certain C runtime library function, which is needed to perform
specific initializations. However, this is platform/compiler specific,
so it's kind of Off Topic.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
program without main () ? robinsonreyna C Programming 13 01-10-2006 04:18 PM
How can to write a c program without a main() Spidey C Programming 37 12-06-2005 12:24 AM
c program without main( ) ? leeaby@gmail.com C Programming 16 07-17-2005 10:56 AM
Question: Program crashs without reaching the breakpoint set at the beginning of main()? wenjie wang C++ 5 11-01-2004 01:14 AM



Advertisments