鱼C论坛

 找回密码
 立即注册
查看: 3280|回复: 7

关于API程序的莫名其妙的错误

[复制链接]
发表于 2012-7-29 18:32:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
大家帮小弟看一下,这段入门的API程序,执行之后总是提示
error LNK2001: unresolved external symbol _main
1 unresolved externals
难道我哪语义出错误了。
  1. #include <windows.h>

  2. /*  Declare Windows procedure  */
  3. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

  4. /*  Make the class name into a global variable  */
  5. char szClassName[ ] = "WindowsApp";

  6. int WINAPI WinMain (HINSTANCE hThisInstance,
  7.                     HINSTANCE hPrevInstance,
  8.                     LPSTR lpszArgument,
  9.                     int nFunsterStil)

  10. {
  11.     HWND hwnd,hwndButton;               /* This is the handle for our window */
  12.     MSG messages;            /* Here messages to the application are saved */
  13.     WNDCLASSEX wincl;        /* Data structure for the windowclass */

  14.     /* The Window structure */
  15.     wincl.hInstance = hThisInstance;
  16.     wincl.lpszClassName = szClassName;
  17.     wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
  18.     wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
  19.     wincl.cbSize = sizeof (WNDCLASSEX);

  20.     /* Use default icon and mouse-pointer */
  21.     wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  22.     wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  23.     wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  24.     wincl.lpszMenuName = NULL;                 /* No menu */
  25.     wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
  26.     wincl.cbWndExtra = 0;                      /* structure or the window instance */
  27.     /* Use Windows's default color as the background of the window */
  28.     wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

  29.     /* Register the window class, and if it fails quit the program */
  30.     if (!RegisterClassEx (&wincl))
  31.         return 0;

  32.     /* The class is registered, let's create the program*/
  33.     hwnd = CreateWindowEx (
  34.            0,                   /* Extended possibilites for variation */
  35.            szClassName,         /* Classname */
  36.            "Windows App",       /* Title Text */
  37.            WS_OVERLAPPEDWINDOW, /* default window */
  38.            CW_USEDEFAULT,       /* Windows decides the position */
  39.            CW_USEDEFAULT,       /* where the window ends up on the screen */
  40.            544,                 /* The programs width */
  41.            375,                 /* and height in pixels */
  42.            HWND_DESKTOP,        /* The window is a child-window to desktop */
  43.            NULL,                /* No menu */
  44.            hThisInstance,       /* Program Instance handler */
  45.            NULL                 /* No Window Creation data */
  46.            );

  47.     /* Make the window visible on the screen */
  48.     ShowWindow (hwnd, nFunsterStil);
  49.     //
  50. hwndButton = CreateWindow(
  51.     "BUTTON",   // predefined class
  52.     "OK",       // button text
  53.     WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // styles

  54.     // Size and position values are given explicitly, because
  55.     // the CW_USEDEFAULT constant gives zero values for buttons.
  56.     30,         // starting x position
  57.     40,         // starting y position
  58.     100,        // button width
  59.     100,        // button height
  60.     hwnd,       // parent window
  61.     NULL,       // No menu
  62.    hThisInstance,
  63.     NULL);      
  64.    
  65.    
  66.    
  67.    
  68.    
  69.    
  70.    
  71.    
  72.    
  73.     /* Run the message loop. It will run until GetMessage() returns 0 */
  74.    
  75.    
  76.    
  77.    
  78.    
  79.    
  80.    while (GetMessage (&messages, NULL, 0, 0))
  81.     {
  82.         /* Translate virtual-key messages into character messages */
  83.         TranslateMessage(&messages);
  84.         /* Send message to WindowProcedure */
  85.         DispatchMessage(&messages);
  86.     }

  87.     /* The program return-value is 0 - The value that PostQuitMessage() gave */
  88.     return messages.wParam;
  89. }


  90. /*  This function is called by the Windows function DispatchMessage()  */

  91. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  92. {
  93.     switch (message)                  /* handle the messages */
  94.     {
  95.         case WM_DESTROY:
  96.             PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
  97.             break;
  98.         default:                      /* for messages that we don't deal with */
  99.             return DefWindowProc (hwnd, message, wParam, lParam);
  100.     }

  101.     return 0;
  102. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-7-29 23:30:20 | 显示全部楼层
你建的工程好像不对。。。一般这样错误的意思是你的主函数写错了。。。那么你就是建立的控制台的工程,然后你写的win32程序。。。所以会报这样的错误   Win32 Application   建立这个工程试一下,就是控制台上面的那个 。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2012-8-6 06:41:34 | 显示全部楼层

我就是建立的 win32API工程啊。 我换了个简单WINAPI程序,但是第一次编译可以成功并可以弹出窗口。但是第二次重新打开这个工程的.cpp程序就出现 error LNK2001: unresolved external symbol _main 和1 unresolved externals。奇怪吧,到底是什么原因呢。你有出现这状况吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-8-6 11:46:59 | 显示全部楼层
一点都不奇怪。。。找到你的这个工程的路径  ,把里面上次编译生成的exe文件和 obj文件 还有 ncp文件什么的那些垃圾文件全部删除掉,只留下。.cpp文件 和 .h文件,然后重新编译。。。。如果还是不行  新建一个工程,把你的这个工程的.cpp整过去。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-8-6 20:08:42 | 显示全部楼层
你第一建完之后,想再打开工程的文件里有个.dsw的吧。每次打开这个就可以了。呵呵。以前我也是不知道。后来就知道了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2012-8-7 07:42:46 | 显示全部楼层
孙九 发表于 2012-8-6 20:08
你第一建完之后,想再打开工程的文件里有个.dsw的吧。每次打开这个就可以了。呵呵。以前我也是不知道。后来 ...

我仔细看了下,有两个.dsw文件,一个是以工程命名的.dsw文件,另一个是选择C++source命名的.dsw文件。如果你点击后一个.dsw文件,还是出现我上面讲的那种错误,但是点击前一个.dsw文件,就没问题了,可以出现编译了。4楼说的那种方法,我是试过了,不行的。 为什么,我在运行控制台程序,每次点击那个.cpp文件就可以重新编译呢,而这里API程序就不行了,想不通。   最后谢谢你们两给我的帮助!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-8-7 14:53:52 | 显示全部楼层
有点晕,O(∩_∩)O哈哈~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-8-7 19:08:44 | 显示全部楼层
seuer126 发表于 2012-8-7 07:42
我仔细看了下,有两个.dsw文件,一个是以工程命名的.dsw文件,另一个是选择C++source命名的.dsw文件。如果 ...

呵呵。能运行了就行。我还以为不行呢。我就是这么做的。估计的别人能知道具体的原因。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 22:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表