Visual Studio 中的 wxWidgets LINK 问题

发布于 2024-12-28 23:43:17 字数 2087 浏览 1 评论 0原文

我第一次尝试处理 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:\Users\550\documents\visual studio 11\Projects\wxWidgitExample\wxWidgitExample\LINK
Error   2   error LNK1120: 1 unresolved externals   C:\Users\550\documents\visual studio 11\Projects\wxWidgitExample\Debug\wxWidgitExample.exe  1

问题是什么?我使用 MS Visual Studio,我想我需要使用 #pragma 指令?

I am trying to deal with wxWidgets for the first time, I tried to compile the below Hello World program:

/*
 * 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);
}

The compiler showed:

Error   1   error LNK2001: unresolved external symbol _WinMainCRTStartup    C:\Users\550\documents\visual studio 11\Projects\wxWidgitExample\wxWidgitExample\LINK
Error   2   error LNK1120: 1 unresolved externals   C:\Users\550\documents\visual studio 11\Projects\wxWidgitExample\Debug\wxWidgitExample.exe  1

What is the problem ? I use MS Visual Studio, I guess I need to use #pragma directive?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相权↑美人 2025-01-04 23:43:17

尝试将 wxDECLARE_APP(MyApp); 放在定义 MyApp 的头文件中,并将 wxIMPLMENT_APP(MyApp); 放在实现它的 cpp 文件中。这些宏的描述可在文档中找到。

Try putting wxDECLARE_APP(MyApp); in the header file where MyApp is defined and wxIMPLEMENT_APP(MyApp); in the cpp file where it is implemented. The description of these macros is avaliable in the documentation.

南城旧梦 2025-01-04 23:43:17

检查链接器 -> 系统 -> 子系统下的属性表。确保子系统是 Windows 而不是 Console。

Check your property sheets under Linker->System->SubSystem. Make sure the subsystem is Windows and not Console.

清晰传感 2025-01-04 23:43:17

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

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

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

如果您的示例不使用 unicode,则使用 /ENTRY:WinMainCRTStartup

如果您的示例使用 unicode,则使用 /ENTRY:wWinMainCRTStartup

That linker error you're getting usually indicates a mismatch in your project configuration settings between ansi vs unicode setup. If you're building your wxApplication with unicode enabled make sure UNICODE and _UNICODE are defined.

The entry point for a windows unicode application is wWinMainCRTStartup where as a non-unicode one has WinMainCRTStartup as its entry point. Normally wxWidgets should have that setup correctly for you when you built the framework.

To help troubleshoot your unresolved error I would try passing this switch to the linker when you attempt to build.

If you're example is not using unicode then use /ENTRY:WinMainCRTStartup.

If you're example is using unicode then use /ENTRY:wWinMainCRTStartup.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文