Visual Studio中的wxWidgets链接问题

wxWidgets LINK issue in Visual Studio

本文关键字:链接 问题 wxWidgets 中的 Studio Visual      更新时间:2023-10-16

我第一次尝试处理wxWidgets,我尝试编译以下Hello World程序:

/*
 * hworld.cpp
 */
#include "wx/wx.h" 
class MyApp: public wxApp
{
    virtual bool OnInit();
};
class MyFrame: public wxFrame
{
public:
    MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
    void OnQuit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);
    DECLARE_EVENT_TABLE()
};
enum
{
    ID_Quit = 1,
    ID_About,
};
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_MENU(ID_Quit, MyFrame::OnQuit)
    EVT_MENU(ID_About, MyFrame::OnAbout)
END_EVENT_TABLE()
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
    MyFrame *frame = new MyFrame( _("Hello World"), wxPoint(50, 50),
                                  wxSize(450,340) );
    frame->Show(true);
    SetTopWindow(frame);
    return true;
} 
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame( NULL, -1, title, pos, size )
{
    wxMenu *menuFile = new wxMenu;
    menuFile->Append( ID_About, _("&About...") );
    menuFile->AppendSeparator();
    menuFile->Append( ID_Quit, _("E&xit") );
    wxMenuBar *menuBar = new wxMenuBar;
    menuBar->Append( menuFile, _("&File") );
    SetMenuBar( menuBar );
    CreateStatusBar();
    SetStatusText( _("Welcome to wxWidgets!") );
}
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
    Close(TRUE);
}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
    wxMessageBox( _("This is a wxWidgets Hello world sample"),
                  _("About Hello World"),
                  wxOK | wxICON_INFORMATION, this);
}

编译器显示:

Error   1   error LNK2001: unresolved external symbol _WinMainCRTStartup    C:Users550documentsvisual studio 11ProjectswxWidgitExamplewxWidgitExampleLINK
Error   2   error LNK1120: 1 unresolved externals   C:Users550documentsvisual studio 11ProjectswxWidgitExampleDebugwxWidgitExample.exe  1

问题出在哪里?我使用MS Visual Studio,我想我需要使用#pragma指令吗?

尝试将wxDECLARE_APP(MyApp);放在定义MyApp的头文件中,将wxIMPLEMENT_APP(MyApp);放在实现它的cpp文件中。文档中提供了这些宏的描述。

检查Linker->System->SubSystem下的属性表。确保子系统是Windows而不是Console。

您收到的链接器错误通常表明项目配置设置中ansi与unicode设置不匹配。如果您在启用unicode的情况下构建wxApplication,请确保定义了UNICODE_UNICODE

windows unicode应用程序的入口点是wWinMainCRTStartup,其中作为非unicode,WinMainCRTStartup是其入口点。通常,wxWidgets应该在构建框架时为您正确设置。

为了帮助解决未解决的错误,我会尝试在您尝试构建时将此开关传递到链接器。

如果您的示例不使用unicode,那么请使用/ENTRY:WinMainCRTStartup

如果是使用unicode的,则使用/ENTRY:wWinMainCRTStartup