Specifying a non default entry point
The following little progam simply creates a message box and returns 42. Thus, it does not need the
CRT and can be linked with
/nodefaultlib
.
#include <windows.h>
int __stdcall entryFunc(void) {
MessageBoxA(NULL, "Hello", "entryFunc", 0);
return 42;
}
Because it does not use the CRT, there is no
main
function either and consequently no default entry point.
Because the linker does not find one of the default entry points, it needs to be specfied with the /ENTRY
option.
Additionally, without default entry point, the linker cannot determine the target
subsystem. Thus, it must be specified with the
/SUBSYSTEM
linker option. Otherwise, the linker will complain with the error message
fatal error LNK1221: a subsystem can't be inferred and must be defined.
The subsystem is chosen to be console
in order to be able to check the exit value of app.exe
with %errorlevel%
.
The Makefile
to create app.exe
:
app.exe: app.obj
link /nodefaultlib app.obj /entry:entryFunc /subsystem:console user32.lib /out:app.exe
app.obj: app.c Makefile
cl /nologo /c /W4 /GS- app.c