今天遇到一个error LNK2001: unresolved external symbol _main

 

#include < windows.h >
#include
< stdio.h >

class  cDataPackage
{
protected:
    
// Data buffer and size
    void *m_Buf;
    unsigned 
long m_Size;
public:
    cDataPackage() 
{ m_Buf = NULL; m_Size = 0; }
    
~cDataPackage() { Free(); }

    
void *Create(unsigned long Size)
    
{
        
// Free a previously created buffer
        Free();
        
// Allocate some memory and return a pointer
        return (m_Buf = (void*)new char[(m_Size = Size)]);
    }


    
// Free the allocated memory
    void Free() { delete m_Buf; m_Buf = NULL; m_Size = 0; }

    
bool Save(char *Filename)
    
{
        FILE 
*fp;
        
// Make sure there’s something to write
        if(m_Buf != NULL && m_Size)
        
{
            
// Open file, write size and data
            if((fp=fopen(Filename, "wb")) != NULL)
            
{
                fwrite(
&m_Size, 14, fp);
                fwrite(m_Buf, 
1, m_Size, fp);
                fclose(fp);
                
return TRUE;
            }

        }

        
return FALSE;
    }


    
void *Load(char *Filename, unsigned long *Size)
    
{
        FILE 
*fp;
        
// Free a prior buffer
        Free();
        
if((fp=fopen(Filename, "rb"))!=NULL)
        
{
            
// Read in size and data
            fread(&m_Size, 14, fp);
            
if((m_Buf = (void*)new char[m_Size]) != NULL)
                fread(m_Buf, 
1, m_Size, fp);
            fclose(fp);
            
// Store size to return
            if(Size != NULL)
                
*Size = m_Size;
            
// return pointer
            return m_Buf;
        }

        
return NULL;
    }

}
;

typedef 
struct   {
    
char Name[32];
}
sName;

int  WINAPI WinMain(HINSTANCE hInst,
                   HINSTANCE hPrev,
                   LPSTR szCmdLine,
                   
int  nCmdShow)
{
    cDataPackage DP;
    DWORD Size;
    
// Create the data package (w/64 bytes) and get the
    
// pointer, casting it to an sName structure type.
    sName *Names = (sName*)DP.Create(64);
    
// Since there are 64 bytes total, and each name uses
    
// 32 bytes, then I can have 2 names stored.
    strcpy(Names[0].Name, "Jim");
    strcpy(Names[
1].Name, "Adams");
    
// Save the names to disk and free the data buffer
    DP.Save("names.dat");
    DP.Free();
    
// Load the names from disk. Size will equal 64
    
// when the load function returns.
    Names = (sName*)DP.Load("names.dat"&Size);
    
// Display the names
    MessageBox(NULL, Names[0].Name, "1st Name", MB_OK);
    MessageBox(NULL, Names[
1].Name, "2nd Name", MB_OK);
    
// Free up the data package
    DP.Free();
    
return 0;
}

 

我先建立一个cpp文件然后在编译的,结果就出现error LNK2001: unresolved external symbol _main怎么也解决不了,后来网上找到了答案:

“直接打开(或编辑)了一个Winmain程序就Build。VC  此时假定你要建立Console程序而不是GUI程序,而Console程序的Startup   Code要载入的是main()而不是Winmain(),导致连接失败。改正的方法是将菜单Project/Settings下Link项Option内的/subsystem:console改为/subsystem:windows。更稳妥的做法是以后先New一个Win32   GUI   Project,再拷贝入你的程序,再Build。”

后来按其所说果然ok!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值