Change entrypoint main() to another name
-
You can't. It's the Linker of your C compiler which looks for a function named "main" to make it the entry point of your program.
In reality things are even more complicated: The actual entry point of your program is not the main() function, but the init function of the C Runtime Library. It's the C Runtime that will call main() after it has completed its own initialization. You can not change that, without changing and re-compiling the C Runtime. And you certainly don't want that, I guess ;-)
You can change the entry point of your executable with some Linker option. But if you change the entry point to some custom function, you are bypassing the C Runtime initialization, so you won't be able to use any C Runtime functions (like printf() and friends), until you have called the C Runtime init function manually! But as soon as you do that, the C Runtime will initialize itself and, after it has completed the init, then call the main() function...
So you should rather fix the library! Why is there a main() function in a library? And if that library is a DLL file, why is main() exported at all?
-
MuldeR thank you
and how i change the linker options so i can use a new entrypoint?
In VS i can set the /ENTRY Point Symbol or can use wmain() or _tmain() (this works in VS).
Why is there a main() function in a library?
it's a normal program from a microcontroller-system in the library and i want to use it in a GUI. The library isn't a dll, it's a static lib (*.a-file) maked with the QT Creator. -
In MSVC _tmain() is just a macro that will either expand to main() or wmain(), depending on whether you make an ANSI or Unicode build. Also, when using /ENTRY to specify a custom entry point, you must call mainCRTStartup() manually, which in turn will then call your main() function. Otherwise the C Runtime is not initialized and evil things are going to happen ;-)
__
In GCC you would use something like this:
@-Wl,-eentry@which should change the entry point to:
@int entry()
{
return 0;
}@But again: This way you will bypass the C Runtime initialization! The entry() function does not even get the command-line arguments. That's because splitting the command-line string into separate tokens and passing them as "argv[]" to the main() function is also done by the C Runtime (at least on Win32), even before your main() function is invoked. You will also note that printf() and friends may not work as expected or simply crash...
Read the complete story here:
http://linuxgazette.net/84/hawk.html_
I mainly see two reasons for using a custom entry point function:
- You write a program that doesn't use the C Standard Library at all, maybe some low-level system driver/daemon or something like that.
- You need to do some "early" initialization stuff, even before the C Runtime takes over control. In this case you would invoke the C Runtime at some point and then continue your "actual" program from main().
__
You should rather change the code of your library like this:
@/** define iff compiling as a library **/
#defined COMPILED_AS_LIB#ifndef COMPILED_AS_LIB
int main(int argc. char argv[])
{
/* Main function here, that will NOT be compiled into the lib **/
}
#endif@__
Alternatively something like this might work:
@#define DLL_PUBLIC attribute ((visibility ("default")))
#define DLL_LOCAL attribute ((visibility ("hidden")))DLL_LOCAL int main(int argc. char argv[])
{
/* Main function that is NOT be visible from "outside" the DLL **/
}@By default, GCC exports every function from the lib, i.e. makes them accessible from outside the DLL. That's the opposite of what MSVC does, where you need an explicit __declspec(dllexport). Above code should prevent the "main" function from being exported...
-
Addendum:
http://dbp-consulting.com/tutorials/debugging/linuxProgramStartup.htmlThis article explains well the relation between _start() and main() :-)