phildsp
asked on
How to redefine main()
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.
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.
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) ??
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) ??
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.
'...own versions of the standard functions,...' I mean.
ASKER
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.
_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.
ASKER
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)
{
}
#if __GNUC__
extern "C" __declspec(dllexport) void* fake_main (void*) asm("main");
#define main fake_main
#endif
void* main(void* arg)
{
}
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 (https://www.experts-exchange.com/help.jsp#hi73).
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
-bcl (bcladd)
EE Cleanup Volunteer
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 (https://www.experts-exchange.com/help.jsp#hi73).
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
-bcl (bcladd)
EE Cleanup Volunteer
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
--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).