Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
See more:
Hi all,
I see the following problem: I compile a project using VC++6.0 with its code:
XML
#include <stdio.h>
#pragma comment(linker,"/ENTRY:ChangedEntry")
void ChangedEntry()
{
    printf("Here is changed entry point!");
}


When I compile it then it appeare two errors:
VB
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/xr.exe : fatal error LNK1120: 1 unresolved externals

In there, the xr is project name.
I don't want using default entrypoint (is main() function) that I want using ChangedEntry() to be entrypoint of program, hope you help me solve this problem, thanks.
Posted

If you are linking with the C runtime then main must still be a linkable entry point.
 
Share this answer
 
The C default library -independently if you're using it or not- contains a function (named mainCRTstartup or similar) that is the default entry point (yes: it is not main. Who says that the program entry point in main is deliberately lying, since he wants you to don't care about how the system internally works)
That function -after initializing all the global objects and calling their constructors- calls main.

So, even if you instruct the linker to tell the OS not to call it, that function exist and places a call to "main" in the symbol table.
As a consequence a function named "main" must exist somewhere to let the linker to resolve that name.

WARNING: What your doing, in fact, makes C++ not to behave anymore as C++, since you have forced the OS to call your function without initializing the global objects before.
If your function (or whatever you call from then on) access whatever (your or library) global object (like, for example, std::cout), may find it in an inconsistent state.
There are pieces of C++ code the must be executed before main enters and after main returns.
 
Share this answer
 
v2
Comments
Dalek Dave 9-Jan-11 14:32pm    
Good Answer.
to solve this error,go to project->setting->Link>project options>you have seen /subsystem:console /incremental:yes
change it /subsystem:windows
Make sure you using
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
instead int main()

:) Happy Coding ...
 
Share this answer
 
Comments
Andrewpeter 3-Feb-14 9:06am    
Thanks. My vote is 5 for your answer.
You have to compile the program as a static or dynamic link library, executables has to expose the main function.
 
Share this answer
 
Hi all,
I've solved this problem, I do the following:
- Step 1: Use Notepad to write the code segment:
XML
#include <stdio.h>
#pragma comment(linker, "/ENTRY:ChangedEntry /NODEFAULTLIB /SUBSYSTEM:CONSOLE")
void ChangedEntry()
{
    printf("Here is changed entry point!");
}


Save it to a file with extended file .c, suppose Test.c
- Step 2: Run cmd and use this command:
cl.exe Test.c user32.lib

OK, I have an app which runs very well, share my solution for you. Have fun, thanks.
 
Share this answer
 
Comments
Espen Harlinn 19-Jan-11 6:37am    
You have learned something, that's good. main is the standard entry point for c and c++ applications - (WinMain for windows applications, something many considers a severe error in some soft tissue computational matrix used by a well-known company in Redmond, USA ). For the sake of your future coworkers adhere to standards, everything else becomes a wee bit confusing down the road :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900