到官网下载相关源码
为了编译wxWidgets,需要安装GTK(wxWidgets依赖)、cmake
1、安装Command Line Tool:xcode-select–install
(如果安装过Xcode,可以忽略这一步)
2、安装Homebrew
使用Ruby脚本安装,Mac自带Ruby,因此直接执行指令:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
3、安装pkg-config
pkg-config是姨哥提供从源代码中编译软件时查询已安装的库使用的统一接口工具。
因为安装好了Homebrew,直接使用Homebrew安装,执行指令:
brew install pkg-config
4、安装GTK+3.0,执行指令:
brew install gtk+3
5、配置pkg-config
vim ~/.bash_profile
在文件中加入以下代码,不同环境下路径会有些许不同,注意修改:
export PKG_CONFIG_PATH="/usr/X11/lib/pkgconfig:/usr/local/Cellar/cairo/1.12.16/lib/pkgconfig/"
保存退出后:
source ~/.bash_profile
测试代码:
#include
#include
#include
static gboolean delete_event(GtkWidget * widget, GdkEvent * event, gpointer data) {
gtk_main_quit();
return FALSE;
}
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *label;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_title(GTK_WINDOW(window), "RobberPhex");
gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
g_signal_connect(window, "delete-event", G_CALLBACK(delete_event), NULL);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
label = gtk_label_new("Hello GTK!");
gtk_container_add(GTK_CONTAINER(window), label);
gtk_widget_show(label);
gtk_widget_show(window);
gtk_main();
return 0;
}
执行指令:
gcc $(pkg-config gtk+-3.0 --cflags) $(pkg-config gtk+-3.0 --libs) gtk_test.c -o gtktest
因为已经安装了Homebrew,直接执行指令:
brew install cmake
$ mkdir wx_build
$ cd wx_build/
$ ../configure --disable-shared --enable-unicode
“-disable-shared”指定构建静态库
$ make
$ make install
$ cd samples/
$ make
$ cd animate/
$ make
$ ./anitest
#include
#include "simple.h"
class Hello : public wxFrame
{
public:
Hello(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 150))
{
Centre();
}
};
class MyApp : public wxApp
{
public:
bool OnInit()
{
Simple *simple = new Simple(wxT("Hello"));
simple->Show(true);
return true;
}
};
wxIMPLEMENT_APP(MyApp);
simple.cpp:
#include "simple.h"
Simple::Simple(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 150))
{
Centre();
}
simple.h:
#ifndef WXWIDGETS_TEST2_SIMPLE_H
#define WXWIDGETS_TEST2_SIMPLE_H
#include "wx/wx.h"
class Simple : public wxFrame
{
public:
Simple(const wxString& title);
};
#endif //WXWIDGETS_TEST2_SIMPLE_H
CMakeList.txt:
cmake_minimum_required(VERSION 3.9)
project(wxWidgets_test2)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp simple.cpp simple.h)
add_executable(wxWidgets_test2 ${SOURCE_FILES})
file(GLOB wxWidgets_LIBRARTIES "/Users/cmzhang/Documents/wxWidgets-3.1.1/wx_build")
find_package(wxWidgets REQUIRED COMPONENTS net core base)
include(${wxWidgets_USE_FILE})
target_link_libraries(wxWidgets_test2 ${wxWidgets_LIBRARIES})
P.S. 使用cmake编译wxWidgets程序,需要在CMakeLists.txt文件中加入以下三句话:
find_package(wxWidgets REQUIRED COMPONENTS net core base)
include(${wxWidgets_USE_FILE})
target_link_libraries(wxWidgets_test2 ${wxWidgets_LIBRARIES})
编译时可能会出现错误:’tr1/type_traits’ file not found,其原因是因为Mac OS X 10.9以后clang使用libc++库而不是libstdc++库,libc++库提供c++11库,而libstdc++提供c++03库支持tr1
解决方法:使用libstd++库编辑build目录下cmake生成的CMakeCache.txt文件,这个文件是cmake重要参数,修改其中的CMAKE_CXX_FLAGS:STRING=为CMAKE_CXX_FLAGS:STRING=-stdlib=libstdc++
http://blog.topspeedsnail.com/archives/3708
http://blog.51cto.com/arithxu/1413680
https://www.jianshu.com/p/4e80b42823d5
http://gohom.win/2015/10/11/pybel_install/
https://blog.csdn.net/jgt_insect/article/details/52201340