用nmake编译链接程序报错error LNK2019:
link /INCREMENTAL:NO /NOLOGO -subsystem:windows,5.0 -out:bin\demo3.exe bin\3.obj kernel32.lib ws2_32.lib mswsock.lib advapi32.lib bufferoverflowu.lib
LIBCMTD.lib(wincrt0.obj) : error LNK2019: 无法解析的外部符号_WinMain@16,该符号在函数 ___tmainCRTStartup 中被引用bin\demo3.exe : fatal error LNK1120: 1 个无法解析的外部命令
NMAKE : fatal error U1077: “"d:\Program Files\Microsoft
Visual Studio 8\VC\BIN\
link.EXE"”: 返回代码“0x460”
Stop.
解决方式:在程序中添加
#pragma comment( linker, "/subsystem:console /entry:mainCRTStartup" )
可已根据需要添加不同组合
#pragma comment( linker, "/subsystem:windows/entry:WinMainCRTStartup" )
#pragma comment( linker, "/subsystem:windows/entry:mainCRTStartup" )
#pragma comment( linker, "/subsystem:console /entry:mainCRTStartup" )
#pragma comment( linker, "/subsystem:console /entry:WinMainCRTStartup" )
==================================================================================
Makefile
!include <Win32.Mak>
OUTDIR = bin
all:$(OUTDIR) $(OUTDIR)\demo3.exe
$(OUTDIR):
if not exist "$(OUTDIR)/$(NULL)" mkdir $(OUTDIR)
$(OUTDIR)\3.obj:3.c
$(CC) $(cflags) $(cvars) /Gz /Fo"$(OUTDIR)\\"
/Fd"$(OUTDIR)" 3.c
$(OUTDIR)\demo3.exe:$(OUTDIR)\3.obj
$(link) $(guiflags) -out:$(OUTDIR)\demo3.exe
$(OUTDIR)\3.obj $(conlibs)
==================================================================================
源文件3.c
#include <windows.h>
#pragma comment( linker, "/subsystem:console /entry:mainCRTStartup" )//新增
int main(int argc, TCHAR argv[])
{
HANDLE hFile;
DWORD dwWritten;
TCHAR szSystemDir[MAX_PATH];
GetSystemDirectory(szSystemDir, MAX_PATH);
hFile = CreateFile("systemroot.txt",
GENERIC_WRITE,
0,NULL,CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
if (!WriteFile(hFile, szSystemDir, lstrlen(szSystemDir), &dwWritten, NULL))
{
return GetLastError();
}
}
CloseHandle(hFile);
return 0;
}
==================================================================================