这段代码编译通过!但连接错误,错误信息为:
Compiling...
showme.cpp
Linking...
libcd.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/showme.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.showme.exe - 2 error(s), 0 warning(s)代码如下:#include "windows.h"
//#include "afx.h"
//#include <afxwin.h>
#include "stdlib.h"
#include "string.h"
#include "math.h"
#include "windowsx.h"#define Pi 3.14159265
unsigned short InitWindowsClass(HINSTANCE hInstance);
bool InitWindows(HINSTANCE hInstance,int nCmdShow);
HWND hWndMain;
long WINAPI WndProc(HWND,UINT,UINT,long );
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hprevInstance,LPSTR lpCmdLine,int nCmdShow)
{
MSG Message;
if(!InitWindowsClass(hInstance))
return FALSE;
if(!InitWindows(hInstance,nCmdShow))
return FALSE;
while(GetMessage(&Message,0,0,0))
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return Message.wParam;
}long WINAPI WndProc(HWND hWnd,UINT iMessage,UINT wParam,long lParam)
{
POINT lpOuterPoints[100];
POINT lpInerPoints[100];
HDC hDC;
HBRUSH hBrush;
HPEN hPen;
PAINTSTRUCT PtStr;
double dfRadious0=100.0,dfRadious1;
POINT lpTriangle[3];
dfRadious1=dfRadious0*sin(0.1*Pi)/sin(126.0/180*Pi);
for (int i=0;i<5;i++)
{
lpOuterPoints[i].x=(long)(dfRadious0*cos(i*72.0/180*Pi));
lpOuterPoints[i].y=(long)(dfRadious0*sin(i*72.0/180*Pi));
lpInerPoints[i].x=(long)(dfRadious1*cos(i*72.0/180*Pi+36.0/180*Pi));
lpInerPoints[i].y=(long)(dfRadious1*sin(i*72.0/180*Pi+36.0/180*Pi));
}
switch(iMessage)
{
case WM_PAINT:
hDC=BeginPaint(hWnd,&PtStr);
SetMapMode(hDC,MM_ANISOTROPIC);
SetWindowOrgEx(hDC,-100,-100,NULL);
hPen=CreatePen(PS_SOLID,1,RGB(255,0,0));
SelectObject(hDC,hPen);
Polygon(hDC,lpOuterPoints,5);
for (i=0;i<5;i++)
{
lpTriangle[0]=lpOuterPoints[i%5];
lpTriangle[1]=lpInerPoints[i%5];
lpTriangle[2]=lpOuterPoints[(i+1)%5];
hBrush=CreateSolidBrush(RGB(i*10,i*20,i*30));
SelectObject(hDC,hBrush);
Polygon(hDC,lpTriangle,3);
lpTriangle[2]=lpInerPoints[(i+4)%5];
hBrush=CreateSolidBrush(RGB(i*40,i*30,i*20));
SelectObject(hDC,hBrush);
Polygon(hDC,lpTriangle,3);
}
hBrush=CreateSolidBrush(RGB(255,255,255));
SelectObject(hDC,hBrush);
Polygon(hDC,lpInerPoints,5);
MoveToEx(hDC,lpOuterPoints[0].x,lpOuterPoints[0].y,NULL);
for(i=1;i<=5;i++)
{
hPen=CreatePen(PS_SOLID,1,RGB(0,,i*51,0));
SelectObject(hDC,hPen);
LineTo(hDC,lpOuterPoints[(i+2)%5].x,lpOuterPoints[(i+2)%5].y);
}
hPen=(HPEN)(GetStockObject(PS_NULL));
DeleteObject(hPen);
DeleteObject(hBrush);
EndPaint(hWnd,&PtStr);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
}
bool InitWindows(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hWnd=CreateWindow("polygon","五边形",WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL);
if(!hWnd) return FALSE;
hWndMain=hWnd;
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
unsigned short InitWindowsClass(HINSTANCE hInstance)
{
WNDCLASS WndClass;
WndClass.cbClsExtra=0;
WndClass.cbWndExtra=0;
WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
WndClass.lpfnWndProc=WndProc;
WndClass.lpszClassName="polygon";
WndClass.lpszMenuName=NULL;
WndClass.style=0;
return  RegisterClass(&WndClass);
}

解决方案 »

  1.   

    这个问题出在你建的工程的性质为console型的,可是你的程序是一个基于用户界面的。下面的话肯定可以解决问题
    One mistake that new Windows/Visual C++ developers commonly make is to accidentally select the wrong project type when they create a new project. For example, a developer might create a new Win32 Application project but create an entry-point function of main. When building the application, the developer will get a linker error because a Win32 Application project sets the /SUBSYSTEM:WINDOWS linker switch but no WinMain or wWinMain function exists. At this point, the developer has four options:
    Change the main function to WinMain. This is usually not the best choice because the developer probably wants to create a console application.
    Create a new Win32 Console Application in Visual C++ and add the existing source code modules to the new project. This option is tedious because it feels like you're starting over and you have to delete the original project file.
    Click on the Link tab of the Project Settings dialog box and change the /SUBSYSTEM:WINDOWS switch to /SUBSYSTEM :CONSOLE. This is an easy way to fix the problem; few people are aware that this is all they have to do.
    Click on the Link tab of the Project Settings dialog box and delete the /SUBSYSTEM:WINDOWS switch entirely. This is my favorite choice because it gives you the most flexibility. Now, the linker will simply do the right thing based on which function you implement in your source code. I have no idea why this isn't the default when you create a new Win32 Application or Win32 Console Application project with Visual C++'s Developer Studio.
      

  2.   

    把DEBUG目录下的所有文件都删除再编译一下试试。