tags:

views:

1292

answers:

2

When compiling my wxWidget HelloWorld application, I am getting the following errors:

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

My source code is as follows:

//

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!"));
}

What is missing?

A: 

Make sure you match setting on your project to what all your dependencies are using (actually you should match dependencies :)).

Settings that can cause linking problems with MS toolchain (besides obvious not linking the libraries at all):

  • Use unicode/multibyte character set
  • Treat wchar_t as built-in type.

When you know that damn unresolved-wchar_t*-containing-symbol is in the damn library you just linked into, this is probably one of those two.

  • Runtime (multi/single threaded [debug] [dll]).

This is the reason for your LIBCMTD warning. And for missing/conflicting symbols like __free or malloc or other standard looking things. And for mysterious no reason crashes when crossing dll boundaries, or even on empty place if you somehow manage to link 2 different runtimes into one binary (I've seen it!) .

  • Suspect preprocessor definitions like _LIB, _DLL, QT_DLL, etc.

Those are used by some libraries to decide if code supposed to be linked statically or dynamically. They usually affect headers that accompany a lib or dll. You have to know if you need them or not. RTFM or look at configs for working example projects for those.

So for your problem, first make sure you add whatever wxWidget libraries you must (and whatever dependencies they need). Search for any of the missing symbols and let google guide you. Somebody would have same problem and would have posted it somewhere before figuring it out themselves.

A good search term is

virtual bool __thiscall wxApp::Initialize

Special care is needed for runtime. When you got all libs you need, but you get libcmt* or msvc* warnings or conflicts then go through all your projects settings and check that 4 items I listed are correct and consistent. You have to know them for dependencies too, if you didn't build them yourself. Use linker verbosity flag too see exactly who brings in unwanted runtime.

Other compiler and linker settings might affect things too, so go with a fine comb through them all.

Most of those changes require clean recompile.

This is the fun of building C++ code.

Eugene
+2  A: 

Looks to me like the error you get when you link you the wrong C Runtime Library. When you build wxWidgets it uses the Multi-threaded DLL option and Multi-threaded Debug DLL options by default for Release and Debug build respectively.

To change this in you app you need to go:

Build->Properties->C/C++->Code Generation and then change the Runtime Library option and rebuild your app.

If you would prefer to statically link against the C Runtime Library so you don't need to DLL you could run a find replace again all of the vcproj files in wxWighets\build\msw and replace

RuntimeLibrary="3" with RuntimeLibrary="1" and

RuntimeLibrary="2" with RuntimeLibrary="0"

This will change DLL builds as well however and this may not be what you want.

SteveL