Program without main

G

Guruz

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?
 
D

Darrell Grainger

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.
 
T

Thomas Matthews

Guruz said:
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
 
D

Dan Pop

In said:
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
 
B

Ben Pfaff

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.
 
M

Mitchell

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top