我刚刚在OS X上使用wxWidgets启动了一个irc客户端项目(10.11.3(15D21))。
然而,当我编译代码并运行它时,由于某种原因它会出错。我用gdb运行程序并获得下面的输出。
Program received signal SIGSEGV, Segmentation fault.
0x00007fff9793871a in std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::assign(std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&) () from /usr/lib/libstdc++.6.dylib
我的代码并没有偏离wxWidgets网站上的hello world教程,所以我不确定为什么我会遇到这个问题。根据gdb输出,问题似乎与std::string::assign()
有关。唯一可以看到这个被调用的地方是wxFrame的构造,其中标题被传入?
main.hpp
#ifndef __main_hpp
#define __main_hpp
#include <sockit.hpp>
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include "frame.hpp"
class IrcClientApp : public wxApp {
public:
virtual bool OnInit();
};
#endif
的main.cpp
#include "main.hpp"
wxIMPLEMENT_APP(IrcClientApp);
bool IrcClientApp::OnInit() {
IrcClientFrame* frame = new IrcClientFrame("IRC client", wxPoint(100, 100), wxSize(600, 400));
frame->Show(true);
return true;
}
frame.hpp
#ifndef __frame_hpp
#define __frame_hpp
class IrcClientFrame : public wxFrame {
private:
void about(wxCommandEvent&);
void exit(wxCommandEvent&);
wxDECLARE_EVENT_TABLE();
public:
IrcClientFrame(const wxString&, const wxPoint&, const wxSize&);
};
#endif
frame.cpp
#include "main.hpp"
wxBEGIN_EVENT_TABLE(IrcClientFrame, wxFrame)
EVT_MENU(wxID_ABOUT, IrcClientFrame::about)
wxEND_EVENT_TABLE()
IrcClientFrame::IrcClientFrame(const wxString& title, const wxPoint& pos, const wxSize& size) :
wxFrame(NULL, wxID_ANY, title, pos, size) {
// About menu item
wxMenu* menu_about = new wxMenu;
menu_about->Append(wxID_ABOUT);
// Menu bar
wxMenuBar* menu_bar = new wxMenuBar;
menu_bar->Append(menu_about, "&Help");
// Set it
SetMenuBar(menu_bar);
CreateStatusBar();
SetStatusText("This is the status text!");
}
void IrcClientFrame::about(wxCommandEvent& e) {
wxMessageBox("Hello world", "Hello world", wxOK | wxICON_INFORMATION);
}
void IrcClientFrame::exit(wxCommandEvent& e) {
Close(true);
}
使用以下Makefile编译
CC=g++
WXCONFIG=$(shell ../../../deps/bin/wx-config --cxxflags --libs)
INCLUDES=-I../../../src
CFLAGS=-g -std=c++11 -Wall -Wno-inconsistent-missing-override $(INCLUDES)
LDFLAGS=-L../../../deps/lib
BIN=../bin/client
CPPS=main.cpp frame.cpp
all: mkdir_bin examples
examples: $(CPPS)
$(CC) $(CFLAGS) $(WXCONFIG) $(CPPS) -o $(BIN)
mkdir_bin: ; if [ ! -d ../bin ]; then mkdir ../bin; fi
clean: ; if [ -d ../bin ]; then rm -rf ../bin; fi
这里是崩溃报告中的调度队列
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000001
Exception Note: EXC_CORPSE_NOTIFY
VM Regions Near 0x1:
-->
__TEXT 0000000103d8d000-0000000103d93000 [ 24K] r-x/rwx SM=COW /Users/USER/*
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libstdc++.6.dylib 0x00007fff9793871a std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::assign(std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&) + 26
1 libwx_osx_cocoau_core-3.1.dylib 0x00000001041bcfbf wxNonOwnedWindow::Create(wxWindow*, int, wxPoint const&, wxSize const&, long, wxString const&) + 63
2 libwx_osx_cocoau_core-3.1.dylib 0x00000001041c1311 wxTopLevelWindowMac::Create(wxWindow*, int, wxString const&, wxPoint const&, wxSize const&, long, wxString const&) + 49
3 client 0x0000000103d912ec wxFrame::wxFrame(wxWindow*, int, wxString const&, wxPoint const&, wxSize const&, long, wxString const&) + 140 (frame.h:35)
4 client 0x0000000103d90d2f IrcClientFrame::IrcClientFrame(wxString const&, wxPoint const&, wxSize const&) + 175 (frame.cpp:8)
5 client 0x0000000103d9111d IrcClientFrame::IrcClientFrame(wxString const&, wxPoint const&, wxSize const&) + 45 (frame.cpp:23)
6 client 0x0000000103d8e9b6 IrcClientApp::OnInit() + 134 (main.cpp:6)
7 libwx_osx_cocoau_core-3.1.dylib 0x00000001041b755e wxApp::CallOnInit() + 158
8 libwx_baseu-3.1.dylib 0x00000001048623f9 wxEntry(int&, wchar_t**) + 121
9 client 0x0000000103d8e8a6 main + 38 (main.cpp:3)
10 libdyld.dylib 0x00007fff9740a5ad start + 1
答案 0 :(得分:3)
您没有使用-std=c++11
构建库,但是将它用于您自己的代码,这可能就是问题所在。 OS X下的推荐方法是使用-std=c++11 -stdlib=libc++
来编译库和你自己的代码,即删除你的旧构建目录(你确实构建在一个单独的目录中,不是吗? ?),然后重新运行配置CXXFLAGS="-std=c++11 -stdlib=libc++"
并生成。最后,更新您自己的makefile以使用-stdlib=libc++
。