我从<windows程序设计>中"抄"了下面的程序,可不能"link":
#include "windows.h"
#include "math.h"
#include "process.h"#define REP 1000000#define STATUS_READY 0
#define STATUS_WORKING 1
#define STATUS_DONE 2
#define WM_CALC_DONE (WM_USER+0)
#define WM_CALC_ABORTED (WM_USER+1)typedef struct
{
HWND hwnd;
BOOL bContinue;
}
PARAMS,*PPARAMS;LRESULT APIENTRY WndProc(HWND,UINT,WPARAM,LPARAM);int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
PSTR szCmdLine,int iCmdShow)
{
static TCHAR szAppName[]=TEXT("超级测试!");
HWND hwnd;
MSG msg;
WNDCLASS wndclass; wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=szAppName; if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("此程序需要NT!"),szAppName,MB_ICONERROR);
return 0;
}
hwnd=CreateWindow(szAppName,TEXT("多线程的测试"),WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,
NULL,hInstance,NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd); while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}void mThread(PVOID pvoid)
{
double A=1.0;
int i;
long lTime;
volatile PPARAMS pparams; pparams=(PPARAMS)pvoid;
lTime=GetCurrentTime();
for(i=0;i<REP&&pparams->bContinue;i++)
A=tan(atan(exp(log(sqrt(A*A)))))+1.0;
if(i==REP)
{
lTime=GetCurrentTime()-lTime;
SendMessage(pparams->hwnd,WM_CALC_DONE,0,lTime);
}
else
SendMessage(pparams->hwnd,WM_CALC_ABORTED,0,0); _endthread();
}LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
static int iStatus;
static long lTime;
static PARAMS params;
static TCHAR* szMessage[]={TEXT("好了,鼠标左键开始"),
TEXT("正在计算,稍侯..."),
TEXT("%d 次计算花费 %ld 毫秒")};
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
TCHAR szBuffer[64];
switch(message)
{
case WM_LBUTTONDOWN:
if(iStatus==STATUS_WORKING)
{
MessageBeep(0);
return 0;
}
iStatus=STATUS_WORKING;
params.hwnd=hwnd;
params.bContinue=TRUE;
_beginthread(mThread,0,¶ms);
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_RBUTTONDOWN:
params.bContinue=TRUE;
return 0;
case WM_CALC_DONE:
lTime=lParam;
iStatus=STATUS_DONE;
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_CALC_ABORTED:
iStatus=STATUS_READY;
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
wsprintf(szBuffer,szMessage[iStatus],REP,lTime);
DrawText(hdc,szBuffer,-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
此程序被称为"野蛮的测试程序",所以A=tan(atan(exp(log(sqrt(A*A)))))+1.0;语句仅是做为一个计算的测试,没有实际的应用意义
请各位看看这是为什么?在编译的时候出现了LNK2001和LNK1120的错误
#include "windows.h"
#include "math.h"
#include "process.h"#define REP 1000000#define STATUS_READY 0
#define STATUS_WORKING 1
#define STATUS_DONE 2
#define WM_CALC_DONE (WM_USER+0)
#define WM_CALC_ABORTED (WM_USER+1)typedef struct
{
HWND hwnd;
BOOL bContinue;
}
PARAMS,*PPARAMS;LRESULT APIENTRY WndProc(HWND,UINT,WPARAM,LPARAM);int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
PSTR szCmdLine,int iCmdShow)
{
static TCHAR szAppName[]=TEXT("超级测试!");
HWND hwnd;
MSG msg;
WNDCLASS wndclass; wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=szAppName; if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("此程序需要NT!"),szAppName,MB_ICONERROR);
return 0;
}
hwnd=CreateWindow(szAppName,TEXT("多线程的测试"),WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,
NULL,hInstance,NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd); while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}void mThread(PVOID pvoid)
{
double A=1.0;
int i;
long lTime;
volatile PPARAMS pparams; pparams=(PPARAMS)pvoid;
lTime=GetCurrentTime();
for(i=0;i<REP&&pparams->bContinue;i++)
A=tan(atan(exp(log(sqrt(A*A)))))+1.0;
if(i==REP)
{
lTime=GetCurrentTime()-lTime;
SendMessage(pparams->hwnd,WM_CALC_DONE,0,lTime);
}
else
SendMessage(pparams->hwnd,WM_CALC_ABORTED,0,0); _endthread();
}LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
static int iStatus;
static long lTime;
static PARAMS params;
static TCHAR* szMessage[]={TEXT("好了,鼠标左键开始"),
TEXT("正在计算,稍侯..."),
TEXT("%d 次计算花费 %ld 毫秒")};
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
TCHAR szBuffer[64];
switch(message)
{
case WM_LBUTTONDOWN:
if(iStatus==STATUS_WORKING)
{
MessageBeep(0);
return 0;
}
iStatus=STATUS_WORKING;
params.hwnd=hwnd;
params.bContinue=TRUE;
_beginthread(mThread,0,¶ms);
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_RBUTTONDOWN:
params.bContinue=TRUE;
return 0;
case WM_CALC_DONE:
lTime=lParam;
iStatus=STATUS_DONE;
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_CALC_ABORTED:
iStatus=STATUS_READY;
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
wsprintf(szBuffer,szMessage[iStatus],REP,lTime);
DrawText(hdc,szBuffer,-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
此程序被称为"野蛮的测试程序",所以A=tan(atan(exp(log(sqrt(A*A)))))+1.0;语句仅是做为一个计算的测试,没有实际的应用意义
请各位看看这是为什么?在编译的时候出现了LNK2001和LNK1120的错误
project->setting->c/c++ -> code Generation -> use run-time library选 Multithreaded有关的。还有你的文件名后缀是c 还是cpp?
你的工程是不是基于win32 console application 建立的,
如果是,就会发生LNK2001和LNK1120的错误,
petzold 的书中的程序需要基于win32 application 建立的。
C/C++ tab
Category : choose Code Generation
Use run-time library:
choose Debug Multithreaded DLL or some other multithreaded version.project->Settings...
link tab
Project Options
/subsystem:windows change to /subsystem:console
link tab
Project Options
/subsystem:console change to /subsystem:windows
I cann't find the following in the link tab:
/subsystem:consoleto apocn() :
My project is a Win32 console application.
How do I with what you said?to atling() :
When I have done with what you said,the error is decrease one.But there is another error.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
你在新建工程时,选择Win32 console application上面的那个选项,即Win32 application.
还有_beginthread(),_endthread(),在process.h中声明,但必须在setting中设置/MT标志,就像上面他们说的那样。petzold在本章的前面已经所明白了。
To masterz():
Why do this?
The project is a win32 console application,but the "console" should change to "windows",I don't understand.
_MT项!!!测试通过!!!
I want to know the reason of the change which masterz() said.