M1 Mac上wxWidgets按钮绑定出现x86_64架构未定义符号错误
wxWidgets按钮绑定出现x86_64架构未定义符号错误的解决方法
问题描述
在运行macOS Sonoma 14.4.1的M1 MacBook Pro上安装wxWidgets后,编译修改Hello World示例时,添加按钮绑定出现以下链接错误:
Undefined symbols for architecture x86_64: "MyFrame::onOkButton(wxCommandEvent&)", referenced from: MyFrame::MyFrame() in hello_tests-3e1e88.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
相关代码如下:
// Start of wxWidgets "Hello World" Program #include <wx/wx.h> #include <wx/button.h> class MyApp : public wxApp { public: bool OnInit() override; }; wxIMPLEMENT_APP(MyApp); class MyFrame : public wxFrame { public: MyFrame(); private: void OnHello(wxCommandEvent& event); void OnExit(wxCommandEvent& event); void OnAbout(wxCommandEvent& event); void onOkButton(wxCommandEvent& event); }; enum { ID_Hello = 1 }; bool MyApp::OnInit() { MyFrame *frame = new MyFrame(); frame->Show(true); return true; } MyFrame::MyFrame() : wxFrame(nullptr, 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!"); //first attempt at adding a widget to the frame - SW 4/20/24 // wxButton* myButton = new wxButton(this, wxID_ANY, ("MyButton" ), wxPoint(10, 20), wxSize(80,30)); Bind(wxEVT_MENU, &MyFrame::OnHello, this, ID_Hello); Bind(wxEVT_MENU, &MyFrame::OnAbout, this, wxID_ABOUT); Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT); //Bind(wxEVT_BUTTON, &MyFrame::onOkButton); //myButton->Bind(wxEVT_BUTTON, &MyFrame::onOkButton, this); } 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!"); } void onOkButton(wxCommandEvent& event) { wxMessageBox("Button Test", "About This App", wxOK | wxICON_INFORMATION); }
问题原因
你在MyFrame类的私有成员中声明了onOkButton成员函数,但实现时没有加上MyFrame::作用域,导致这个函数被当成全局函数,而绑定代码引用的是MyFrame类的成员函数,链接器找不到对应的符号。
修复步骤
- 修改
onOkButton的实现,添加类作用域:
void MyFrame::onOkButton(wxCommandEvent& event) { wxMessageBox("Button Test", "About This App", wxOK | wxICON_INFORMATION); }
- 取消按钮绑定代码的注释,推荐使用按钮自身的Bind方式(逻辑更清晰):
myButton->Bind(wxEVT_BUTTON, &MyFrame::onOkButton, this);
修改后重新编译,链接器就能找到MyFrame::onOkButton的定义,错误即可解决。
内容的提问来源于stack exchange,提问作者SJWoodward
相关产品推荐
相关产品推荐

