如何在 Ubuntu/Debian/Linux Mint 中编译和安装 wxWidgets
wxWidgets
wxWidgets是一个程序开发框架/库, 允许你在Windows、Mac、Linux中使用相同的代码跨平台开发。它主要用C++写成,但也可以与其他语言绑定比如Python、Perl、Ruby。
本教程中我将向你展示如何在基于Debian的linux中如Ubuntu和Linux Mint中编译wxwidgets 3.0+。
从源码编译wxWidgets并不困难,仅仅需要几分钟。库可以按不同的方式来编译,比如静态或者动态库。
1. 下载 wxWidgets
第一步你需要从wxwidgets.org下载wxWidgets源码文件。
做完后,解压到目录。
2. 设置编译环境
要编译wxwidgets,我们需要一些工具包括C++编译器,在Linux上是g++。所有这些可以通过apt-get工具从仓库中安装。
我们还需要wxWidgets依赖的GTK开发库。
<span class="pln">$ sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install libgtk</span><span class="pun">-</span><span class="lit">3</span><span class="pun">-</span><span class="pln">dev build</span><span class="pun">-</span><span class="pln">essential checkinstall</span>
这个叫做checkinstall的工具允许我们为wxwidgets创建一个安装包,这样之后就可以轻松的使用包管理器来卸载。
3. 编译 wxWidgets
进入到wxWidgets解压后的目录。为了保持清洁,创建一个编译用的目录。
<span class="pln">$ mkdir gtk</span><span class="pun">-</span><span class="pln">build</span>
<span class="pln">$ cd gtk</span><span class="pun">-</span><span class="pln">build</span><span class="pun">/</span>
现在运行configure和make命令。每个将花费一些时间来完成。
<span class="pln">$ </span><span class="pun">../</span><span class="pln">configure </span><span class="pun">--</span><span class="pln">disable</span><span class="pun">-</span><span class="pln">shared </span><span class="pun">--</span><span class="pln">enable</span><span class="pun">-</span><span class="pln">unicode</span>
<span class="pln">$ make</span>
"--disable-shared"选项将会编译静态库而不是动态库。
make命令完成后,编译就成功了。是时候安装wxWidgets到正确的目录。
更多信息请参考install.txt和readme.txt,这可在wxwidgets中的/docs/gtk/目录下找到。
4. 安装 checkinstall
现在我们不使用"make install"命令,我们使用checkinstall命令来创建一个wxwidgets的deb安装包。运行命令:
<span class="pln">$ sudo checkinstall</span>
checkinstall会询问几个问题,请保证在提问后提供一个版本号,否则将会失败。
完成这一切后,wxWidgets就安装好了,deb文件也会创建在相同的目录下。
5. 追踪安装的文件
如果你想要检查文件安装的位置,使用dpkg命令后面跟上checkinstall提供的包名。
<span class="pln">$ dpkg </span><span class="pun">-</span><span class="pln">L package_name</span>
<span class="pun">/.</span>
<span class="pun">/</span><span class="pln">usr</span>
<span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span>
<span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">lib</span>
<span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">libwx_baseu</span><span class="pun">-</span><span class="lit">3.0</span><span class="pun">.</span><span class="pln">a</span>
<span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">libwx_gtk3u_propgrid</span><span class="pun">-</span><span class="lit">3.0</span><span class="pun">.</span><span class="pln">a</span>
<span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">libwx_gtk3u_html</span><span class="pun">-</span><span class="lit">3.0</span><span class="pun">.</span><span class="pln">a</span>
<span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">libwxscintilla</span><span class="pun">-</span><span class="lit">3.0</span><span class="pun">.</span><span class="pln">a</span>
<span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">libwx_gtk3u_ribbon</span><span class="pun">-</span><span class="lit">3.0</span><span class="pun">.</span><span class="pln">a</span>
<span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">libwx_gtk3u_stc</span><span class="pun">-</span><span class="lit">3.0</span><span class="pun">.</span><span class="pln">a</span>
<span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">libwx_gtk3u_qa</span><span class="pun">-</span><span class="lit">3.0</span><span class="pun">.</span><span class="pln">a</span>
<span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">libwx_baseu_net</span><span class="pun">-</span><span class="lit">3.0</span><span class="pun">.</span><span class="pln">a</span>
<span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">libwxtiff</span><span class="pun">-</span><span class="lit">3.0</span><span class="pun">.</span><span class="pln">a</span>
6. 编译示例
编译wxWidgets完成后就可以马上编译示例程序了。在相同的目录下,一个新的sample目录已经创建了。
进入它并运行下面的命令
<span class="pln">$ compile samples</span>
<span class="pln">$ cd samples</span><span class="pun">/</span>
<span class="pln">$ make</span>
make命令完成后,进入sample 子目录,这里就有一个可以马上运行的Demo程序了。
7. 编译你的第一个程序
你完成编译demo程序后,可以写你自己的程序来编译了。这个也很简单。
假设你用的是C++,这样的话你还可以使用编辑器的高亮特性。比如gedit、kate、kwrite等等。或者用全功能的IDE像Geany、Codelite、Codeblocks等等。
然而你的第一个程序只需要用一个文本编辑器来快速完成。
如下:
<span class="com">#include</span><span class="str"><wx/wx.h></span>
<span class="kwd">class</span><span class="typ">Simple</span><span class="pun">:</span><span class="kwd">public</span><span class="pln"> wxFrame</span>
<span class="pun">{</span>
<span class="kwd">public</span><span class="pun">:</span>
<span class="typ">Simple</span><span class="pun">(</span><span class="kwd">const</span><span class="pln"> wxString</span><span class="pun">&</span><span class="pln"> title</span><span class="pun">)</span>
<span class="pun">:</span><span class="pln"> wxFrame</span><span class="pun">(</span><span class="pln">NULL</span><span class="pun">,</span><span class="pln"> wxID_ANY</span><span class="pun">,</span><span class="pln"> title</span><span class="pun">,</span><span class="pln"> wxDefaultPosition</span><span class="pun">,</span><span class="pln"> wxSize</span><span class="pun">(</span><span class="lit">250</span><span class="pun">,</span><span class="lit">150</span><span class="pun">))</span>
<span class="pun">{</span>
<span class="typ">Centre</span><span class="pun">();</span>
<span class="pun">}</span>
<span class="pun">};</span>
<span class="kwd">class</span><span class="typ">MyApp</span><span class="pun">:</span><span class="kwd">public</span><span class="pln"> wxApp</span>
<span class="pun">{</span>
<span class="kwd">public</span><span class="pun">:</span>
<span class="kwd">bool</span><span class="typ">OnInit</span><span class="pun">()</span>
<span class="pun">{</span>
<span class="typ">Simple</span><span class="pun">*</span><span class="pln">simple </span><span class="pun">=</span><span class="kwd">new</span><span class="typ">Simple</span><span class="pun">(</span><span class="pln">wxT</span><span class="pun">(</span><span class="str">"Simple"</span><span class="pun">));</span>
<span class="pln">simple</span><span class="pun">-></span><span class="typ">Show</span><span class="pun">(</span><span class="kwd">true</span><span class="pun">);</span>
<span class="kwd">return</span><span class="kwd">true</span><span class="pun">;</span>
<span class="pun">}</span>
<span class="pun">};</span>
<span class="pln">wxIMPLEMENT_APP</span><span class="pun">(</span><span class="typ">MyApp</span><span class="pun">);</span>
现在保存并用下面的命令编译。
<span class="com"># compile</span>
<span class="pln">$ g</span><span class="pun">++</span><span class="pln"> basic</span><span class="pun">.</span><span class="pln">cpp </span><span class="str">`wx-config --cxxflags --libs std`</span><span class="pun">-</span><span class="pln">o program</span>
<span class="com"># run</span>
<span class="pln">$ </span><span class="pun">./</span><span class="pln">program</span>
和非标准的库一起编译
面展示的wx-config命令默认只支持标准的库。如果你使用的是Aui库,那么你需要指定额外用到的库。
<span class="pln">$ g</span><span class="pun">++</span><span class="pln"> code</span><span class="pun">.</span><span class="pln">cpp </span><span class="str">`wx-config --cxxflags --libs std,aui`</span><span class="pun">-</span><span class="pln">o program</span>
更多的信息参考这里这里。
资源
下载wxWidgets的源码和帮助 https://www.wxwidgets.org/downloads/
wxWidgets编译的wiki页面 https://wiki.wxwidgets.org/Compilingandgetting_started
使用wxWidgets最新版本(3.0+)的事项 https://wiki.wxwidgets.org/UpdatingtotheLatestVersionofwxWidgets
wxWidgets 的详细介绍:请点这里
wxWidgets 的下载地址:请点这里
相关阅读:
via: http://www.binarytides.com/install-wxwidgets-ubuntu/
作者:Silver Moon 译者:geekpi 校对:wxy