读书人

VC++菜鸟编译不通过

发布时间: 2013-08-10 21:14:06 作者: rapoo

VC++初学者编译不通过,求助啊
在看刘冰的C++程序设计,运行书上70页,ex03_3,无法编译通过,求助高手是什么问题?
我建立的Win32控制台工程,错误提示:
Error1error LNK2001: unresolved external symbol _mainMSVCRT.lib
Error2fatal error LNK1120: 1 unresolved externals

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
LPTSTR szAppName = "简单的Windows应用程序";
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
if(!hPrevInstance)
{
wndclass.style = CS_VREDRAW | CS_HREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;

if(!RegisterClass(&wndclass))
return FALSE;
}

hwnd = CreateWindow(
szAppName,
szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);

ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);

while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;

switch(message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

------解决方案--------------------


int _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )

把这一行的入口函数的名字改下,再加个API的关键字。具体参考默认的Win32入口函数写法吧。_tWinMain看起都像MFC的写法。你用Win32就用标准的Win32的入口函数写法呗。
int WINAPI WinMain(  HINSTANCE hInstance,      // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state);

还有MSDN是个很强大的东西,学会用对你学VC很有好处哦。
(注:关于_tWinMainde意义我也不是太清楚,希望有高手来指点下哈)
[解决办法]
改为GUI工程,而不是控制台工程。

引用:
在看刘冰的C++程序设计,运行书上70页,ex03_3,无法编译通过,求助高手是什么问题?
我建立的Win32控制台工程,错误提示:
Error1error LNK2001: unresolved external symbol _mainMSVCRT.lib
Error2fatal error LNK1120: 1 unresolved externals

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
LPTSTR szAppName = "简单的Windows应用程序";
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
if(!hPrevInstance)
{
wndclass.style = CS_VREDRAW
[解决办法]
CS_HREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;

if(!RegisterClass(&wndclass))
return FALSE;
}

hwnd = CreateWindow(
szAppName,
szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);

ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);

while(GetMessage(&msg, NULL, 0, 0))


{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;

switch(message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

读书人网 >C++

热点推荐