C ++: wxWidget HelloWorld
When compiling the wwWidget HelloWorld application, the following errors occur:
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 original code looks like this:
//
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?
source to share
Make sure you map the options in your project to what all your dependencies use (you should actually map dependencies :)).
Settings that can cause problems with linking to the MS toolchain program (apart from the obvious not linking libraries at all):
- Use Unicode / Multibyte character set
- Treat wchar_t as a built-in type.
When you know that a damn unresolved-wchar_t * -containing symbol is in the damn library you just linked to, it's probably one of two things.
- Runtime (multi / single threaded [debug] [dll]).
This is the reason for your LIBCMTD warning. And for missing / conflicting characters like __free or malloc or other standard stuff. And for the mysterious reasons for crashes when crossing dll boundaries or even from scratch if you manage to bundle 2 different modes of operation into one binary (I've seen it!).
- Suspicious preprocessor definitions like _LIB, _DLL, QT_DLL, etc.
These libraries are used by some libraries to decide whether the code should be linked statically or dynamically. They usually affect the headers that accompany the lib or dll. You need to know if you need them or not. RTFM or see configurations for working projects for them.
So, for your problem, first make sure you add all the wxWidget libraries you need (and any dependencies they need). Find any of the missing characters and give it to google guide. Someone would have the same problem and post it somewhere before figuring it out.
Good search term
virtual bool __thiscall wxApp::Initialize
Special help is required to work. When you get all the libraries you need, but you get libcmt * or msvc * warnings or conflicts, then go through all the parameters of your projects and make sure the 4 items I listed are correct and consistent. You should know them for dependencies too, if you didn't build them yourself. Use the linker verbosity flag, also see exactly who is bringing the unwanted workspace.
Other compiler and linker settings can affect things too, so go through them all.
Most of these changes require a clean recompilation.
It's a pleasure to create C ++ code.
source to share
This is similar to the error you get when you reference the wrong C runtime library. When you create wxWidgets, it uses the Multi-threaded DLL option and the default Debug DLL multithreaded options to build Release and Debug respectively.
To change this in your application, you need to go:
Build-> Properties-> C / C ++ -> Code Generation and then change the Runtime Library setting and rebuild the app.
If you prefer to statically reference the C runtime library so that you don't need to use the DLL, you can run find to replace all vcproj files in wxWighets \ build \ msw again and replace
RuntimeLibrary = "3" with RuntimeLibrary = "1" and
RuntimeLibrary = "2" with RuntimeLibrary = "0"
This will also change the DLL assemblies, but that might not be what you want.
source to share