Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how #define a main() function and call our own main function?

Reply
Thread Tools

how #define a main() function and call our own main function?

 
 
ravi
Guest
Posts: n/a
 
      09-25-2007
On Sep 25, 2:09 pm, Richard Heathfield <r...@see.sig.invalid> wrote:
> user923005 said:
>
> <snip>
>
> > The main() function cannot be called from a user defined library.

>
> C&V please.
>
> > The main() function is special. It defines the entry point for
> > program startup.

>
> Agreed. Er, so? What stops you putting it in a library? Surely it's all one
> as far as the linker is concerned?
>
> --
> Richard Heathfield <http://www.cpax.org.uk>
> Email: -http://www. +rjh@
> Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
> "Usenet is a strange place" - dmr 29 July 1999


I can write a main function in another file and compile it as a
library link it to another program where there is no main , but some
sub functions which are called by main. and it works well.

 
Reply With Quote
 
 
 
 
karthikbalaguru
Guest
Posts: n/a
 
      09-25-2007
On Sep 25, 2:17 pm, ravi <gototh...@gmail.com> wrote:
> On Sep 25, 2:09 pm, Richard Heathfield <r...@see.sig.invalid> wrote:
>
>
>
>
>
> > user923005 said:

>
> > <snip>

>
> > > The main() function cannot be called from a user defined library.

>
> > C&V please.

>
> > > The main() function is special. It defines the entry point for
> > > program startup.

>
> > Agreed. Er, so? What stops you putting it in a library? Surely it's all one
> > as far as the linker is concerned?

>
> > --
> > Richard Heathfield <http://www.cpax.org.uk>
> > Email: -http://www. +rjh@
> > Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
> > "Usenet is a strange place" - dmr 29 July 1999

>
> I can write a main function in another file and compile it as a
> library link it to another program where there is no main , but some
> sub functions which are called by main. and it works well.- Hide quoted text -
>
> - Show quoted text -


How is this possible ?

Thx,
Karthik Balaguru

 
Reply With Quote
 
 
 
 
Laurent Deniau
Guest
Posts: n/a
 
      09-25-2007
On 25 sep, 07:03, ravi <gototh...@gmail.com> wrote:
> Hello everybody,
>
> I am writing a small application which does some work before the user
> main function starts execution.
>
> I am trying to #define the main function.
>
> But the problem is that,
>
> the main () function by user may be of different types
> 1. main()


not conformant -> int main(void)

> 2.main(int argc)


not conformant

> 3. main(int argc, char *argv[])



// prelude, already posted here many times
#define COS_PP_CAT(a,b) COS_PP_CAT_(a,b)
#define COS_PP_CAT_(a,b) a ## b
#define COS_PP_NARG_(...) COS_PP_NARG_N_(__VA_ARGS__)
#define COS_PP_NARG_N_( \
_1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
_11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
_21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
_31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
_41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
_51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
_61,_62,_63,N,...) N
#define COS_PP_CAT_NARG(a,...) COS_PP_CAT(a,COS_PP_NARG(__VA_ARGS__))

// your problem
#define main(...) \
COS_PP_CAT_NARG(MAIN_,__VA_ARGS__)(__VA_ARGS__)

#define MAIN_1() \
main(void) { \
int user_main(void); \
your_main(0,0); \
user_main(); \
} \
int user_main(void)

#define MAIN_2(ARGC,ARGV) \
main(int argc, char *argv[]) { \
int user_main(ARGC,ARGV); \
your_main(argc,argv); \
user_main(argc,argv); \
}
int user_main(ARGC,ARGV)

int main(int my_argc, char *my_argc[])
{
// code using my_argc and my_argv after your_main() has been called.
}

I just wrote this code on the fly, so it is untested and assumes the
availability of c99 variadic macros.

I use very often this kind of macro dispatch based on its number of
argument in COS (macros with optional arguments). PP_NARG has been
left as posted here in the past, but you could reduce the maximum
number of argument for you case.

a+, ld.

 
Reply With Quote
 
ravi
Guest
Posts: n/a
 
      09-25-2007
On Sep 25, 3:27 pm, karthikbalaguru <karthikbalagur...@gmail.com>
wrote:
> On Sep 25, 2:17 pm, ravi <gototh...@gmail.com> wrote:
>
>
>
> > On Sep 25, 2:09 pm, Richard Heathfield <r...@see.sig.invalid> wrote:

>
> > > user923005 said:

>
> > > <snip>

>
> > > > The main() function cannot be called from a user defined library.

>
> > > C&V please.

>
> > > > The main() function is special. It defines the entry point for
> > > > program startup.

>
> > > Agreed. Er, so? What stops you putting it in a library? Surely it's all one
> > > as far as the linker is concerned?

>
> > > --
> > > Richard Heathfield <http://www.cpax.org.uk>
> > > Email: -http://www. +rjh@
> > > Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
> > > "Usenet is a strange place" - dmr 29 July 1999

>
> > I can write a main function in another file and compile it as a
> > library link it to another program where there is no main , but some
> > sub functions which are called by main. and it works well.- Hide quoted text -

>
> > - Show quoted text -

>
> How is this possible ?
>
> Thx,
> Karthik Balaguru


give a trial.
Write a file with main() function calling a function foo().
main.c :
int main()
{
printf("MAIN\n");
foo();
return 0;
}

Compile it and make it a library like this:
> gcc -c main.c -o main.o
> ar -rc libmain.a main.o


Now write a file foo.c with foo() function.
foo.c:
void foo()
{
printf("FOOOOOOOOO\n");
}
Now compile like this:
> gcc foo.c -lmain

it works and u can try running.

--
Ravi.T

 
Reply With Quote
 
karthikbalaguru
Guest
Posts: n/a
 
      09-25-2007
On Sep 25, 3:45 pm, ravi <gototh...@gmail.com> wrote:
> On Sep 25, 3:27 pm, karthikbalaguru <karthikbalagur...@gmail.com>
> wrote:
>
>
>
>
>
> > On Sep 25, 2:17 pm, ravi <gototh...@gmail.com> wrote:

>
> > > On Sep 25, 2:09 pm, Richard Heathfield <r...@see.sig.invalid> wrote:

>
> > > > user923005 said:

>
> > > > <snip>

>
> > > > > The main() function cannot be called from a user defined library.

>
> > > > C&V please.

>
> > > > > The main() function is special. It defines the entry point for
> > > > > program startup.

>
> > > > Agreed. Er, so? What stops you putting it in a library? Surely it's all one
> > > > as far as the linker is concerned?

>
> > > > --
> > > > Richard Heathfield <http://www.cpax.org.uk>
> > > > Email: -http://www. +rjh@
> > > > Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
> > > > "Usenet is a strange place" - dmr 29 July 1999

>
> > > I can write a main function in another file and compile it as a
> > > library link it to another program where there is no main , but some
> > > sub functions which are called by main. and it works well.- Hide quoted text -

>
> > > - Show quoted text -

>
> > How is this possible ?

>
> > Thx,
> > Karthik Balaguru

>
> give a trial.
> Write a file with main() function calling a function foo().
> main.c :
> int main()
> {
> printf("MAIN\n");
> foo();
> return 0;
> }
>
> Compile it and make it a library like this:
> > gcc -c main.c -o main.o
> > ar -rc libmain.a main.o

>
> Now write a file foo.c with foo() function.
> foo.c:
> void foo()
> {
> printf("FOOOOOOOOO\n");
> }
> Now compile like this:
> > gcc foo.c -lmain

> it works and u can try running.
>
> --
> Ravi.T- Hide quoted text -
>
> - Show quoted text -


How do u say that it works ?
What did u get on the console ?

Karthik Balaguru

 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      09-25-2007
karthikbalaguru <> writes:

> On Sep 25, 2:17 pm, ravi <gototh...@gmail.com> wrote:
>> On Sep 25, 2:09 pm, Richard Heathfield <r...@see.sig.invalid> wrote:
>>
>>
>>
>>
>>
>> > user923005 said:

>>
>> > <snip>

>>
>> > > The main() function cannot be called from a user defined library.

>>
>> > C&V please.

>>
>> > > The main() function is special. It defines the entry point for
>> > > program startup.

>>
>> > Agreed. Er, so? What stops you putting it in a library? Surely it's all one
>> > as far as the linker is concerned?

>>
>> > --
>> > Richard Heathfield <http://www.cpax.org.uk>
>> > Email: -http://www. +rjh@
>> > Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
>> > "Usenet is a strange place" - dmr 29 July 1999

>>
>> I can write a main function in another file and compile it as a
>> library link it to another program where there is no main , but some
>> sub functions which are called by main. and it works well.- Hide quoted text -
>>
>> - Show quoted text -

>
> How is this possible ?


You should ask "why would it NOT be possible"?

If main calls 3 functions - a(),b() and c() what's to stop you linking in
different versions of these functions for the same main() for different purposes?
 
Reply With Quote
 
ravi
Guest
Posts: n/a
 
      09-25-2007
Thanks Deniau.

On Sep 25, 3:42 pm, Laurent Deniau <Laurent.Den...@gmail.com> wrote:
> On 25 sep, 07:03, ravi <gototh...@gmail.com> wrote:
>
> > Hello everybody,

>
> > I am writing a small application which does some work before the user
> > main function starts execution.

>
> > I am trying to #define the main function.

>
> > But the problem is that,

>
> > the main () function by user may be of different types
> > 1. main()

>
> not conformant -> int main(void)
>
> > 2.main(int argc)

>
> not conformant
>
> > 3. main(int argc, char *argv[])

>
> // prelude, already posted here many times
> #define COS_PP_CAT(a,b) COS_PP_CAT_(a,b)
> #define COS_PP_CAT_(a,b) a ## b
> #define COS_PP_NARG_(...) COS_PP_NARG_N_(__VA_ARGS__)
> #define COS_PP_NARG_N_( \
> _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
> _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
> _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
> _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
> _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
> _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
> _61,_62,_63,N,...) N



Where r u calling this macro COS_PP_NARG_N_(__VA_ARGS__).
How does it replace _1/_2/_3......... how it detects the count of
arguments?
please clarify me.


> #define COS_PP_CAT_NARG(a,...) COS_PP_CAT(a,COS_PP_NARG(__VA_ARGS__))
>
> // your problem
> #define main(...) \
> COS_PP_CAT_NARG(MAIN_,__VA_ARGS__)(__VA_ARGS__)
>
> #define MAIN_1() \
> main(void) { \
> int user_main(void); \
> your_main(0,0); \
> user_main(); \
> } \
> int user_main(void)
>
> #define MAIN_2(ARGC,ARGV) \
> main(int argc, char *argv[]) { \
> int user_main(ARGC,ARGV); \
> your_main(argc,argv); \
> user_main(argc,argv); \
> }
> int user_main(ARGC,ARGV)
>
> int main(int my_argc, char *my_argc[])
> {
> // code using my_argc and my_argv after your_main() has been called.
>
> }
>
> I just wrote this code on the fly, so it is untested and assumes the
> availability of c99 variadic macros.
>
> I use very often this kind of macro dispatch based on its number of
> argument in COS (macros with optional arguments). PP_NARG has been
> left as posted here in the past, but you could reduce the maximum
> number of argument for you case.
>
> a+, ld.



 
Reply With Quote
 
ravi
Guest
Posts: n/a
 
      09-25-2007
Thanks Deniau.

On Sep 25, 3:42 pm, Laurent Deniau <Laurent.Den...@gmail.com> wrote:
> On 25 sep, 07:03, ravi <gototh...@gmail.com> wrote:
>
> > Hello everybody,

>
> > I am writing a small application which does some work before the user
> > main function starts execution.

>
> > I am trying to #define the main function.

>
> > But the problem is that,

>
> > the main () function by user may be of different types
> > 1. main()

>
> not conformant -> int main(void)
>
> > 2.main(int argc)

>
> not conformant
>
> > 3. main(int argc, char *argv[])

>
> // prelude, already posted here many times
> #define COS_PP_CAT(a,b) COS_PP_CAT_(a,b)
> #define COS_PP_CAT_(a,b) a ## b
> #define COS_PP_NARG_(...) COS_PP_NARG_N_(__VA_ARGS__)
> #define COS_PP_NARG_N_( \
> _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
> _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
> _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
> _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
> _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
> _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
> _61,_62,_63,N,...) N



Where r u calling this macro COS_PP_NARG_N_(__VA_ARGS__).
How does it replace _1/_2/_3......... how it detects the count of
arguments?
please clarify me.


> #define COS_PP_CAT_NARG(a,...) COS_PP_CAT(a,COS_PP_NARG(__VA_ARGS__))
>
> // your problem
> #define main(...) \
> COS_PP_CAT_NARG(MAIN_,__VA_ARGS__)(__VA_ARGS__)
>
> #define MAIN_1() \
> main(void) { \
> int user_main(void); \
> your_main(0,0); \
> user_main(); \
> } \
> int user_main(void)
>
> #define MAIN_2(ARGC,ARGV) \
> main(int argc, char *argv[]) { \
> int user_main(ARGC,ARGV); \
> your_main(argc,argv); \
> user_main(argc,argv); \
> }
> int user_main(ARGC,ARGV)
>
> int main(int my_argc, char *my_argc[])
> {
> // code using my_argc and my_argv after your_main() has been called.
>
> }
>
> I just wrote this code on the fly, so it is untested and assumes the
> availability of c99 variadic macros.
>
> I use very often this kind of macro dispatch based on its number of
> argument in COS (macros with optional arguments). PP_NARG has been
> left as posted here in the past, but you could reduce the maximum
> number of argument for you case.
>
> a+, ld.



 
Reply With Quote
 
Laurent Deniau
Guest
Posts: n/a
 
      09-25-2007
On 25 sep, 14:11, ravi <gototh...@gmail.com> wrote:
> Thanks Deniau.
>
> On Sep 25, 3:42 pm, Laurent Deniau <Laurent.Den...@gmail.com> wrote:
>
>
>
> > On 25 sep, 07:03, ravi <gototh...@gmail.com> wrote:

>
> > > Hello everybody,

>
> > > I am writing a small application which does some work before the user
> > > main function starts execution.

>
> > > I am trying to #define the main function.

>
> > > But the problem is that,

>
> > > the main () function by user may be of different types
> > > 1. main()

>
> > not conformant -> int main(void)

>
> > > 2.main(int argc)

>
> > not conformant

>
> > > 3. main(int argc, char *argv[])

>
> > // prelude, already posted here many times
> > #define COS_PP_CAT(a,b) COS_PP_CAT_(a,b)
> > #define COS_PP_CAT_(a,b) a ## b
> > #define COS_PP_NARG_(...) COS_PP_NARG_N_(__VA_ARGS__)
> > #define COS_PP_NARG_N_( \
> > _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
> > _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
> > _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
> > _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
> > _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
> > _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
> > _61,_62,_63,N,...) N

>
> Where r u calling this macro COS_PP_NARG_N_(__VA_ARGS__).
> How does it replace _1/_2/_3......... how it detects the count of
> arguments?
> please clarify me.


Oops, I usual with untested code some macros are missing (googleing
this newsgroup or comp.std.c would have pointed my OP on this macro
some years ago):

#define COS_PP_NARG(...) COS_PP_NARG_(__VA_ARGS__,COS_PP_REVSEQ_N(),)
#define COS_PP_REVSEQ_N() \
63,62,61, \
60,59,58,57,56,55,54,53,52,51, \
50,49,48,47,46,45,44,43,42,41, \
40,39,38,37,36,35,34,33,32,31, \
30,29,28,27,26,25,24,23,22,21, \
20,19,18,17,16,15,14,13,12,11, \
10, 9, 8, 7, 6, 5, 4, 3, 2, 1

>
> > #define COS_PP_CAT_NARG(a,...) COS_PP_CAT(a,COS_PP_NARG(__VA_ARGS__))

>
> > // your problem
> > #define main(...) \
> > COS_PP_CAT_NARG(MAIN_,__VA_ARGS__)(__VA_ARGS__)

>
> > #define MAIN_1() \
> > main(void) { \
> > int user_main(void); \
> > your_main(0,0); \
> > user_main(); \
> > } \
> > int user_main(void)

>
> > #define MAIN_2(ARGC,ARGV) \
> > main(int argc, char *argv[]) { \
> > int user_main(ARGC,ARGV); \
> > your_main(argc,argv); \
> > user_main(argc,argv); \
> > }
> > int user_main(ARGC,ARGV)

>
> > int main(int my_argc, char *my_argc[])
> > {
> > // code using my_argc and my_argv after your_main() has been called.

>
> > }

>
> > I just wrote this code on the fly, so it is untested and assumes the
> > availability of c99 variadic macros.

>
> > I use very often this kind of macro dispatch based on its number of
> > argument in COS (macros with optional arguments). PP_NARG has been
> > left as posted here in the past, but you could reduce the maximum
> > number of argument for you case.

>
> > a+, ld.



 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      09-25-2007
On Mon, 24 Sep 2007 22:49:37 -0700, ravi wrote:

> Can you please try this program:
> example.c:
> int main(int argc)
> {
> printf("Hello World\n");
> return 0;
> }
> I am able to compile and run this.


Can you please try this program:
#include <time.h>
#include <stdio.h>
size_t main(struct tm argc)
{
return puts("hello, world") < 0 ? 0x17 : !mktime(&argc);
}
I am able to compile and run this.
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
What will happen if main called in side main function? Ravi C Programming 17 04-01-2006 10:08 PM
Can I have 2 IP addresses on our internal interface on our cisco pix firewall bgamblin@spvg.com Cisco 1 09-08-2005 08:54 PM
How do we stamp our names onto our photos? Kim Digital Photography 6 01-06-2005 06:12 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57