?
Solved

How to redefine main()

Posted on 2003-02-26
8
Medium Priority
?
316 Views
Last Modified: 2010-05-18
Hello,

I'm trying to use gcc/g++ on Win98 to develop against a
fairly well used API.  But the API requires a non ISO
main() inside a DLL such as void* main(void*).  Gcc
doesn't seem to allow you to do this directly.  VC++ and
Borland compilers have no problems in this regard but
using them is not so attractive for work that involves
other platforms.

Is there a work around?  Can the linker be induced to
rename symbols?  Or is assembly code the only way to get
around this, maybe something like:

extern "C" void* fake_main(void*)
{
   asm (".align 4");
   asm (".global main");
   asm ("main:  nop");

   // the real main code here
   return main();
}

I don't really understand the why some of the above
ops would be necessary, if they are.  That code comes
mostly from embedded code examples I've seen.

0
Comment
Question by:phildsp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 8025366
Changing the linker options to explicitly specify an entry point should work, e.g. add

--entry=fake_main

From the 'ld' manpage:

       `--entry=entry'
           Use entry as the explicit symbol for beginning  execu­
           tion  of  your  program, rather than the default entry
           point.  If there is no symbol named entry, the  linker
           will  try  to parse entry as a number, and use that as
           the entry address (the number will be  interpreted  in
           base  10;  you  may use a leading 0x for base 16, or a
           leading 0 for base 8).
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8026205
jkr .. if we redefine the entry point .. does it have to be of same type as main .. like   having arguments argc and argv..
and in case if we can have privilege of having our own TYPE of main .. how wud we pass the custom arguments
suppose my_fake_main is expecting (double,int) ??
0
 
LVL 1

Expert Comment

by:Mathematix
ID: 8028570
Are you happy with namespaces? Defining your own namespaces will allow you to define your own functions, but I must admit that I am not sure if std::main() (if that's what it is) *must* be included in your code.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Expert Comment

by:Mathematix
ID: 8028583
'...own versions of the standard functions,...' I mean.
0
 

Author Comment

by:phildsp
ID: 8031668
Hmmm.  Interesting things happen with --entry.  When I add --entry=_main (which I used instead of fake_main) I get a
_WinMain@12 undefined error.  There is of course a WinMain() function in the code or DllMain() I can't remember.

When I use --entry=main the linker warns that it can't find main but will use the default address.  That produces
a DLL which has main as the symbol.  I'll try the DLL out
in the host application tonight.

Otherwise I'm unfamiliar with namespaces but will look into that.  Thanks for all your help so far.
0
 

Author Comment

by:phildsp
ID: 8040134
Got it!  The following seems to be the simplest and most elegant solution:

#if __GNUC__
extern "C" __declspec(dllexport) void* fake_main (void*) asm("main");
#define main fake_main
#endif

void* main(void* arg)
{
}
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9846909
No comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Answered by original poster: Archive and refund

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (http://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0
 

Accepted Solution

by:
Chmod earned 0 total points
ID: 9892313
PAQed, with points refunded (80)

Chmod
Community Support Moderator
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question