编译成功连接错误的程序

TSE 2003-07-06 06:53:20
下面的程序编译没有问题,连接的时候提示有两个错误,我刚学c++,自己找不出错误的地方,请大家帮一下忙
#include <windows.h>

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

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("rand") ;
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 = szAppName;
wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}

hwnd = CreateWindow (szAppName, // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters

ShowWindow (hwnd, iCmdShow) ;
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)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
int randl;
TCHAR szBuffer[10];

switch (message)
{


case WM_CREATE:
return 0 ;

case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
for (randl=0;randl<5;randl++) {
TextOut(hdc,0,randl*15,szBuffer,wsprintf(szBuffer,TEXT("%d"),rand()));
}
EndPaint (hwnd, &ps) ;
return 0 ;

case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
...全文
29 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dbrave 2003-07-06
  • 打赏
  • 举报
回复
高手!
frankMasson 2003-07-06
  • 打赏
  • 举报
回复
这是你在另一个贴子里的问题而产生的新问题吧,我刚看到了
我猜的果然正确,你就接照我说的方法修改一下工程设置就行

简单地说你建的是一个 console 的程序,却使用了一个 windows 的 main 函数进入点,造成了LNK2001错误.

TSE 2003-07-06
  • 打赏
  • 举报
回复
错误提示为:
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/randwin.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

randwin.exe - 2 error(s), 0 warning(s)
frankMasson 2003-07-06
  • 打赏
  • 举报
回复
另外,楼主用了 rand()函数,应该加上 stdlib.h 这个头文件.
frankMasson 2003-07-06
  • 打赏
  • 举报
回复
我看了一个,整个程序的语法和所用的函数的都没有什么问题,是用标准的的win32应用程序框架改写的.于是我猜楼主是使用了不正确的 /SUBSYSTEM ,造成了LNK2001错误,这是初学者最容易犯的错误了.

1.打开此项目的“属性页”对话框。2.单击“链接器”文件夹。 3.单击“系统”属性页。4.修改“子系统(Subsystem)”属性,你会看到/SUBSYSTEM:CONSOLE,将其改为: /SUBSYSTEM:WINDOWS, 即可.
foxmail 2003-07-06
  • 打赏
  • 举报
回复
下次给出出错信息
应该是工程没建好吧

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧