c program without main( ) ?

L

leeaby

Hi, I will appreciate your assistance.

Can we write a c code which do not contain main()
I have heard that this is possible. Is it really possible?

Thanks for your help in advance

lee
 
S

Sensei

Hi, I will appreciate your assistance.

Can we write a c code which do not contain main()
I have heard that this is possible. Is it really possible?


Yes, a library would do that.
 
R

Richard Heathfield

Hi, I will appreciate your assistance.

Can we write a c code which do not contain main()
I have heard that this is possible. Is it really possible?

Yes, of course. Win32 programmers do it all the time.

C implementations which specify an entry point other than main are generally
called "freestanding" implementations, and requirements on them are far
less severe than on hosted implementations, this laxity over the name of
the entry point being one example of the lower requirements.

(In Win32 programs, the entry point is typically called WinMain.)

A more common situation in which we write C code without main() is when
we're writing a code library, rather than an actual program. The library is
built separately, without main(), and then linked to whichever programs
wish to use it.
 
E

Emmanuel Delahaye

Hi, I will appreciate your assistance.

Can we write a c code which do not contain main()
I have heard that this is possible. Is it really possible?

It's not possible to write a conforming C program without a main(). But
the main() function could be embedded in some 'framework library' (Like
a GUI Windows application for example).

I do that when I write code for dev-C++ :

I have a 'framework.c' containing the main() function :

(missing code at http://mapage.noos.fr/emdel/clib.htm)

#include "ed/inc/sysalloc.h"
#include <string.h>
int main_ (int argc, char *argv[]);

static int is_opt (char const *const sopt, int const argc, char *const
*const argv)
{
int ok = 0;
int i;

for (i = 1; i < argc; i++)
{
if (strcmp (sopt, argv) == 0)
{
ok = 1;
break;
}
}
return ok;
}

static void onexit (void)
{
sys_mem_trace ();
system ("pause");
}

int main (int argc, char *argv[])
{
static char Trace[1 << 11];
int trace = TRACE_OFF;

atexit (onexit);

if (argc > 1)
{
if (is_opt ("/t", argc, argv))
{
trace = TRACE_ON;
}
}

sys_mem_init (Trace, sizeof Trace, trace, NULL);

main_ (argc, argv);

return 0;
}


and in my 'main.c', I have :

#define main main_
#if 0
#include "ed/inc/sysalloc.h"
#include "ed/inc/sys.h"
#undef assert
#define assert(e) ASSERT (e)
#else
#include <assert.h>
#endif
/*
----------------------------------------------------------------------
*/

int main ()
{

/* device under test */

return 0;
}


BTW, why do you ask ? What exactly is your goal ?

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
 
M

Mark

Richard Heathfield said:
Yes, of course. Win32 programmers do it all the time.
C implementations which specify an entry point other than main are
generally
called "freestanding" implementations, and requirements on them are far
less severe than on hosted implementations, this laxity over the name of
the entry point being one example of the lower requirements.
(In Win32 programs, the entry point is typically called WinMain.)
A more common situation in which we write C code without main() is when
we're writing a code library, rather than an actual program. The library
is
built separately, without main(), and then linked to whichever programs
wish to use it.

I've encountered situations in which main() was defined by a library which
provided an external function for the application to hook into. That
sucked!
Fortunately that code now resides in India!

Mark
 
M

Mark

Emmanuel Delahaye said:
It's not possible to write a conforming C program without a main(). But
the main() function could be embedded in some 'framework library' (Like a
GUI Windows application for example).

I do that when I write code for dev-C++ :
<snip garbage>

It would be cleaner if you were to provide an initialization function which
could be called on startup, why didn't you? What happens when the code
you want to test needs to link with another shitty library which also
decided
to define main() ?

Just curious,
Mark
 
E

Emmanuel Delahaye

Mark wrote on 15/07/05 :
<snip garbage>

Garbage ? Thanks...
It would be cleaner if you were to provide an initialization function which
could be called on startup, why didn't you? What happens when the code
you want to test needs to link with another shitty library which also decided
to define main() ?

Just curious,

Linker error ?

This code is designed to test code (like the one posted by people I try
to help on the forums.

It happens that some of this code can be strange (to me), like having
several return from main() or a lot of exit()'s spread out. This is the
way I found to handle that. If you have a better solution, I'd be glad
to see it.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
 
M

Mark

Emmanuel Delahaye said:
Mark wrote on 15/07/05 :

Garbage ? Thanks...
OK, a little harsh, sorry.
Linker error ?

This code is designed to test code (like the one posted by people I try to
help on the forums.

It happens that some of this code can be strange (to me), like having
several return from main() or a lot of exit()'s spread out. This is the
way I found to handle that. If you have a better solution, I'd be glad to
see it.

I've already given you the suggestion, here it is again:
The solution would be to rename your 'main()' function
calling it something intuitive, such as init_testlib();

Then when you want to help someone, you can still copy and paste their code
and need only to add 1 line to the main() function (a call to
init_testlib(argc, argv);)

As for multiple return statements and calls to exit... it makes no
difference to your
library as you have registered yourself with the atexit() function... you
don't perform
ANY calculations or do a cleanup of any sort when main returns control to
you!
Should you have such a requirement in the future, I'd suggest creating
another
function to perform the post-run tasks.

Regards,
Mark
 
S

S.Tobias

Richard Heathfield said:
Yes, of course. Win32 programmers do it all the time.

C implementations which specify an entry point other than main are generally
called "freestanding" implementations, and requirements on them are far
less severe than on hosted implementations, this laxity over the name of
the entry point being one example of the lower requirements.

(In Win32 programs, the entry point is typically called WinMain.)

So you mean to say that Windows+MSVC is a freestanding implementation?
 
R

Richard Heathfield

S.Tobias said:
So you mean to say that Windows+MSVC is a freestanding implementation?

Depends how you invoke the compiler. If you tell it "console app", then no,
it's hosted all right. But yes, if you say to MSVC "this one's a GUI
program", it's basically a freestanding imp. Which explains a lot. :)
 
A

Artie Gold

Richard said:
Depends how you invoke the compiler. If you tell it "console app", then no,
it's hosted all right. But yes, if you say to MSVC "this one's a GUI
program", it's basically a freestanding imp. Which explains a lot. :)
Freefalling?
--ag
 
N

Nigel Horne

Hi, I will appreciate your assistance.

Can we write a c code which do not contain main()
I have heard that this is possible. Is it really possible?

Yes - just write your own replacement to crtStart
 
W

Walter Roberson

Yes - just write your own replacement to crtStart

crtStart is far from portable. I don't even recognize the implementation.
Nothing I've ever worked with.
 
M

Malcolm

Nigel Horne said:
Yes - just write your own replacement to crtStart
Almost never will a hosted implementation initiate a program by calling
main() directly. usually there is some housekeeping / startup code called
first. Sometimes it is possible to override this with the C compiler,
sometimes not.
The name of the actual startup module may well be crtStart on your system,
but this isn't a standard or even a conventional name (on old DOS compilers
it used to be _main).
 
C

Chris Hills

Hi, I will appreciate your assistance.

Can we write a c code which do not contain main()
I have heard that this is possible. Is it really possible?

Thanks for your help in advance

lee

In freestanding program (no OS, usually embedded systems) there is no
requirement for main in the standard.

However as all freestanding programs require some start up assembler
before the C to set up memory spaces, stack etc this code usually ends
with a jump to label "main" by convention not requirement.

You are free to change this to any label you like and some do. However
some debugging tools, notably ICE, often require that there is a main
for their debugging.
 
C

Chris Hills

Malcolm said:
Almost never will a hosted implementation initiate a program by calling
main() directly. usually there is some housekeeping / startup code called
first. Sometimes it is possible to override this with the C compiler,
sometimes not.

Don't you mean freestanding?
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top