簡體   English   中英

C ++:wxWidget HelloWorld

[英]C++ : wxWidget HelloWorld

編譯wxWidget HelloWorld應用程序時,出現以下錯誤:

Warning 1   warning LNK4098: defaultlib 'LIBCMTD' conflicts with use of other libs; use /NODEFAULTLIB:library   wxWidget__HelloWorld    wxWidget__HelloWorld
Error   2   error LNK2001: unresolved external symbol "public: virtual bool __thiscall wxApp::Initialize(int &,wchar_t * *)" (?Initialize@wxApp@@UAE_NAAHPAPA_W@Z)  minimal.obj wxWidget__HelloWorld
Error   3   error LNK2001: unresolved external symbol "public: virtual void __thiscall wxAppConsole::OnAssertFailure(wchar_t const *,int,wchar_t const *,wchar_t const *,wchar_t const *)" (?OnAssertFailure@wxAppConsole@@UAEXPB_WH000@Z)  minimal.obj wxWidget__HelloWorld
Error   4   error LNK2001: unresolved external symbol "public: virtual void __thiscall wxAppConsole::OnAssert(wchar_t const *,int,wchar_t const *,wchar_t const *)" (?OnAssert@wxAppConsole@@UAEXPB_WH00@Z) minimal.obj wxWidget__HelloWorld
Error   5   error LNK2019: unresolved external symbol "protected: void __thiscall wxStringBase::InitWith(wchar_t const *,unsigned int,unsigned int)" (?InitWith@wxStringBase@@IAEXPB_WII@Z) referenced in function "public: __thiscall wxStringBase::wxStringBase(wchar_t const *)" (??0wxStringBase@@QAE@PB_W@Z)   minimal.obj wxWidget__HelloWorld
Error   6   error LNK2019: unresolved external symbol "public: int __cdecl wxString::Printf(wchar_t const *,...)" (?Printf@wxString@@QAAHPB_WZZ) referenced in function "public: void __thiscall MyFrame::OnAbout(class wxCommandEvent &)" (?OnAbout@MyFrame@@QAEXAAVwxCommandEvent@@@Z)    minimal.obj wxWidget__HelloWorld
Error   7   error LNK2001: unresolved external symbol "wchar_t const * const wxEmptyString" (?wxEmptyString@@3PB_WB)    minimal.obj wxWidget__HelloWorld
Error   8   error LNK2001: unresolved external symbol "wchar_t const * const wxStatusLineNameStr" (?wxStatusLineNameStr@@3QB_WB)    minimal.obj wxWidget__HelloWorld
Error   9   error LNK2001: unresolved external symbol "wchar_t const * const wxFrameNameStr" (?wxFrameNameStr@@3QB_WB)  minimal.obj wxWidget__HelloWorld
Error   10  error LNK2019: unresolved external symbol "void __cdecl wxOnAssert(wchar_t const *,int,char const *,wchar_t const *,wchar_t const *)" (?wxOnAssert@@YAXPB_WHPBD00@Z) referenced in function "public: __thiscall wxStringBase::wxStringBase(class wxStringBase const &)" (??0wxStringBase@@QAE@ABV0@@Z)  minimal.obj wxWidget__HelloWorld
Error   11  fatal error LNK1120: 9 unresolved externals F:\C++\_2008_\wxWidget__HelloWorld\Debug\wxWidget__HelloWorld.exe   wxWidget__HelloWorld

我的源代碼如下:

//

Name:        minimal.cpp
// Purpose:     Minimal wxWidgets sample
// Author:      Julian Smart

#include "wx/wx.h"

// Declare the application class
class MyApp : public wxApp
{
public:
    // Called on application startup
    virtual bool OnInit();
};

// Declare our main frame class
class MyFrame : public wxFrame
{
public:
    // Constructor
    MyFrame(const wxString& title);

    // Event handlers
    void OnQuit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);

private:
    // This class handles events
    DECLARE_EVENT_TABLE()
};

// Implements MyApp& GetApp()
DECLARE_APP(MyApp)

// Give wxWidgets the means to create a MyApp object
IMPLEMENT_APP(MyApp)

// Initialize the application
bool MyApp::OnInit()
{
    // Create the main application window
    MyFrame *frame = new MyFrame(wxT("Minimal wxWidgets App"));

    // Show it
    frame->Show(true);

    // Start the event loop
    return true;
}

// Event table for MyFrame
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
    EVT_MENU(wxID_EXIT,  MyFrame::OnQuit)
END_EVENT_TABLE()

void MyFrame::OnAbout(wxCommandEvent& event)
{
    wxString msg;
    msg.Printf(wxT("Hello and welcome to %s"),
               wxVERSION_STRING);


    wxMessageBox(msg, wxT("About Minimal"),
                 wxOK | wxICON_INFORMATION, this);
}

void MyFrame::OnQuit(wxCommandEvent& event)
{
    // Destroy the frame
    Close();
}

#include "mondrian.xpm"

MyFrame::MyFrame(const wxString& title)
       : wxFrame(NULL, wxID_ANY, title)
{
    // Set the frame icon
    SetIcon(wxIcon(mondrian_xpm));

    // Create a menu bar
    wxMenu *fileMenu = new wxMenu;

    // The "About" item should be in the help menu
    wxMenu *helpMenu = new wxMenu;
    helpMenu->Append(wxID_ABOUT, wxT("&About...\tF1"),
                     wxT("Show about dialog"));

    fileMenu->Append(wxID_EXIT, wxT("E&xit\tAlt-X"),
                     wxT("Quit this program"));

    // Now append the freshly created menu to the menu bar...
    wxMenuBar *menuBar = new wxMenuBar();
    menuBar->Append(fileMenu, wxT("&File"));
    menuBar->Append(helpMenu, wxT("&Help"));

    // ... and attach this menu bar to the frame
    SetMenuBar(menuBar);

    // Create a status bar just for fun
    CreateStatusBar(2);
    SetStatusText(wxT("Welcome to wxWidgets!"));
}

什么東西少了?

確保將項目上的設置與所有依賴項都使用的匹配(實際上,您應該匹配依賴項:))。

可能導致與MS工具鏈發生鏈接問題的設置(除了顯然根本不鏈接庫之外):

  • 使用Unicode /多字節字符集
  • 將wchar_t視為內置類型。

當您知道您剛剛鏈接到的該死的庫中該死的unresolved-wchar_t *-contains-symbol時,這可能是這兩個之一。

  • 運行時(多/單線程[調試] [dll])。

這是發出LIBCMTD警告的原因。 對於缺少/沖突的符號(如__free或malloc或其他標准外觀的東西)。 出於神秘的原因,跨越dll邊界時甚至在空曠的地方也不會崩潰,如果您設法以某種方式將2個不同的運行時鏈接到一個二進制文件中(我見過!)。

  • 懷疑預處理器定義,例如_LIB,_DLL,QT_DLL等。

一些庫使用它們來決定代碼應該靜態還是動態鏈接。 它們通常會影響lib或dll附帶的標頭。 您必須知道是否需要它們。 RTFM或查看適用於這些示例項目的配置。

因此,對於您的問題,首先請確保添加了必須的wxWidget庫(以及它們需要的依賴項)。 搜索任何缺少的符號,然后讓Google指導您。 有人會遇到同樣的問題,並且會在自己弄清楚之前將其發布到某個地方。

一個好的搜索詞是

virtual bool __thiscall wxApp::Initialize

運行時需要特別注意。 當您獲得所有所需的庫時,但是收到libcmt *或msvc *警告或沖突,然后檢查所有項目設置,並檢查列出的4個項目是否正確和一致。 如果您自己沒有構建它們,則還必須了解它們的依賴性。 也可以使用鏈接器詳細程度標志來確切查看誰帶來了不必要的運行時。

其他編譯器和鏈接器設置也可能會影響事情,因此請仔細梳理它們。

這些更改大多數都需要重新編譯。

這是構建C ++代碼的樂趣。

在我看來,當您鏈接錯誤的C運行時庫時遇到的錯誤是。 生成wxWidgets時,默認情況下,它分別針對Release和Debug版本使用Multi-threaded DLL選項和Multi-threaded Debug DLL選項。

要在您的應用中更改此設置,您需要執行以下操作:

構建->屬性-> C / C ++->代碼生成,然后更改運行時庫選項並重新構建您的應用程序。

如果您希望靜態鏈接到C運行時庫,從而不需要DLL,則可以再次運行find replace替換wxWighets \\ build \\ msw中的所有vcproj文件,然后替換

RuntimeLibrary =“ 3”與RuntimeLibrary =“ 1”和

RuntimeLibrary =“ 2”和RuntimeLibrary =“ 0”

但是,這也會改變DLL的構建,而這可能並不是您想要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM