Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to redefine main()

Posted on 2003-02-26
8
Medium Priority
?
318 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
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Industry Leaders: 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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

581 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