[英]WxWidgets Hello World fails to compile: “wchar_t must be available”
當我嘗試從MS Visual Studio 2017 Community Edition中的wxWidgets編譯hello world應用程序時,出現大量錯誤。 我認為關鍵錯誤是#error: "wchar_t must be available"
。 據我看,只有一個人發現了此錯誤,當他們詢問時,他們沒有得到任何有用的答案。 [ http://www.cplusplus.com/forum/windows/197245/]
如何使該程序成功編譯,以及如何確保在未來的(實際)項目中不會出現相同的錯誤?
我剛剛開始學習wxWidgets,並在筆記本電腦上安裝了3.0.4版。 我下載了安裝程序,然后按照說明使用Visual Studio來編譯庫。 我相信,由於所有示例項目都已編譯並成功運行,因此安裝成功完成。 但是,當我嘗試在自己的項目中編譯提供的hello world應用程序時,出現了上述錯誤。 我確保已鏈接wxWidgets標頭,盡管由於我不熟悉使用非內置庫,所以可能會犯一個錯誤。 我不確定為什么hello world無法使用示例項目。
// wxWidgets "Hello World" Program
// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
class MyFrame : public wxFrame
{
public:
MyFrame();
private:
void OnHello(wxCommandEvent& event);
void OnExit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
};
enum
{
ID_Hello = 1
};
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame();
frame->Show(true);
return true;
}
MyFrame::MyFrame()
: wxFrame(NULL, wxID_ANY, "Hello World")
{
wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
"Help string shown in status bar for this menu item");
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT);
wxMenu *menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append(menuFile, "&File");
menuBar->Append(menuHelp, "&Help");
SetMenuBar(menuBar);
CreateStatusBar();
SetStatusText("Welcome to wxWidgets!");
Bind(wxEVT_MENU, &MyFrame::OnHello, this, ID_Hello);
Bind(wxEVT_MENU, &MyFrame::OnAbout, this, wxID_ABOUT);
Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT);
}
void MyFrame::OnExit(wxCommandEvent& event)
{
Close(true);
}
void MyFrame::OnAbout(wxCommandEvent& event)
{
wxMessageBox("This is a wxWidgets Hello World example",
"About Hello World", wxOK | wxICON_INFORMATION);
}
void MyFrame::OnHello(wxCommandEvent& event)
{
wxLogMessage("Hello world from wxWidgets!");
}
這是https://docs.wxwidgets.org/trunk/overview_helloworld.html上可用代碼的精確副本,以防萬一網站上的代碼被更新了。
如您所料,我希望程序能夠正常編譯並運行,但是相反,我收到了大量錯誤,此圖中列出了所有錯誤: https : //media.discordapp.net/attachments/452995181667418119/ 527236184326275113 / unknown.png
這是我所做的一些有趣的觀察:
大多數錯誤源於編譯器無法找到類wxFrame的定義
wxApp類仍處於定義狀態,我可以從中繼承myApp類。
chartype.h
給出了一個錯誤,即wchar_t必須可用,即使根據http://www.cplusplus.com/reference/cwchar/wchar_t/而言,它也是c ++內置的,並且我不需要包含任何其他標頭。
在以下代碼段中找到chartype.h
的錯誤:
/* Unicode support requires wchar_t */
#if !wxUSE_WCHAR_T
#error "wchar_t must be available"
#endif /* Unicode */
任何幫助表示贊賞。
經過一番搜索,我發現wxWidgets沒有定義wxUSE_WCHAR_T,但是它確實定義了SIZEOF_WCHAR_T。 對我來說似乎很奇怪。 但是,經過更多搜索后,我發現此頁面: https : //wiki.wxwidgets.org/Microsoft_Visual_C%2B%2B_Guide
從這里看來重要的是:預處理程序定義:
這也使我想起來我很愚蠢,因為在某個時間點上,我閱讀了有關Windows Api的入門教程,並且明確指出如果您打算使用UNICODE,請務必定義UNICODE。
#define UNICODE // Controls the windows library definitions
#define _UNICODE // Controls the C library definitions
我不確定100%,但是我懷疑這可能是問題所在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.