何彦熹 發表於 2020-5-26 12:00:00

Ubuntu安装GTK+教程

<h3 id="step-1"><span style="font-size: 18px">Step 1</span></h3>
<h4 id="修改清华源修改完可提高下载速度"><span style="font-size: 18px">修改清华源(修改完可提高下载速度)</span></h4>
<p><span style="font-size: 18px">先运行</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 18px">sudo gedit /etc/apt/sources.list</span></pre>
</div>
<p><span style="font-size: 18px">替换文本内容,保存,退出。</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 18px"><span style="color: rgba(0, 0, 0, 1)"># 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse</span>
# deb-src https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse</span>
deb https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse</span>
# deb-src https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse</span>
deb https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse</span>
# deb-src https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse</span>
deb https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse</span>
# deb-src https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse</span>
<span style="color: rgba(0, 0, 0, 1)">
# 预发布软件源,不建议启用
# deb https:</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse</span>
# deb-src https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse</span></span></pre>
</div>
<p><span style="font-size: 18px">运行一下命令更新一下。</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 18px">sudo apt-<span style="color: rgba(0, 0, 255, 1)">get</span> update</span></pre>
</div>
<h3 id="step-2"><span style="font-size: 18px">Step 2:</span></h3>
<h4 id="安装gtk"><span style="font-size: 18px">安装GTK</span></h4>
<div class="cnblogs_code">
<pre><span style="font-size: 18px"><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1安装gcc/g++/gdb/make 等基本编程工具</span>
sudo apt-<span style="color: rgba(0, 0, 255, 1)">get</span> install build-<span style="color: rgba(0, 0, 0, 1)">essential

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2安装 libgtk2.0-dev libglib2.0-dev 等开发相关的库文件</span>
sudo apt-<span style="color: rgba(0, 0, 255, 1)">get</span> install gnome-core-<span style="color: rgba(0, 0, 0, 1)">devel

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3用于在编译GTK程序时自动找出头文件及库文件位置</span>
sudo apt-<span style="color: rgba(0, 0, 255, 1)">get</span> install pkg-<span style="color: rgba(0, 0, 0, 1)">config

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">4安装 devhelp GTK文档查看程序</span>
sudo apt-<span style="color: rgba(0, 0, 255, 1)">get</span><span style="color: rgba(0, 0, 0, 1)"> install devhelp

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">5安装 gtk/glib 的API参考手册及其它帮助文档</span>
sudo apt-<span style="color: rgba(0, 0, 255, 1)">get</span> install libglib2.<span style="color: rgba(128, 0, 128, 1)">0</span>-doc libgtk2.<span style="color: rgba(128, 0, 128, 1)">0</span>-<span style="color: rgba(0, 0, 0, 1)">doc

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">6安装基于GTK的界面GTK是开发Gnome窗口的c/c++语言图形库 </span>
sudo apt-<span style="color: rgba(0, 0, 255, 1)">get</span> install glade libglade2-<span style="color: rgba(0, 0, 0, 1)">dev

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">7安装gtk2.0 或者 将gtk+2.0所需的所有文件统通下载安装完毕</span>
sudo apt-<span style="color: rgba(0, 0, 255, 1)">get</span> install libgtk2.<span style="color: rgba(128, 0, 128, 1)">0</span>-dev</span></pre>
</div>
<h3 id="step-3"><span style="font-size: 18px">Step 3</span></h3>
<h4 id="验证是否安装成功"><span style="font-size: 18px">验证是否安装成功</span></h4>
<div class="cnblogs_code">
<pre><span style="font-size: 18px"><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1查看 2.x 版本</span>
pkg-config --modversion gtk+-<span style="color: rgba(128, 0, 128, 1)">2.0</span>

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2查看pkg-config的版本</span>
pkg-config --<span style="color: rgba(0, 0, 0, 1)">version

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3查看是否安装了gtk</span>
pkg-config --list-all grep gtk</span></pre>
</div>
<p>&nbsp;</p>
<h4 id="用一个简单的程序测试一下"><span style="font-size: 18px">用一个简单的程序测试一下</span></h4>
<div class="cnblogs_code">
<pre><span style="font-size: 18px"><span style="color: rgba(0, 128, 128, 1)"> 1</span> #include &lt;gtk/gtk.h&gt;
<span style="color: rgba(0, 128, 128, 1)"> 2</span>
<span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 0, 255, 1)">int</span> main(<span style="color: rgba(0, 0, 255, 1)">int</span> argc, <span style="color: rgba(0, 0, 255, 1)">char</span> *<span style="color: rgba(0, 0, 0, 1)">argv[])
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span> <span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span>   GtkWidget   *<span style="color: rgba(0, 0, 0, 1)">window;
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span>   GtkWidget   *<span style="color: rgba(0, 0, 0, 1)">label;
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span>   
<span style="color: rgba(0, 128, 128, 1)"> 8</span>   gtk_init(&amp;argc,&amp;<span style="color: rgba(0, 0, 0, 1)">argv);
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span>   
<span style="color: rgba(0, 128, 128, 1)">10</span>   window =<span style="color: rgba(0, 0, 0, 1)"> gtk_window_new(GTK_WINDOW_TOPLEVEL);
</span><span style="color: rgba(0, 128, 128, 1)">11</span>   gtk_window_set_title(GTK_WINDOW(window),<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">title</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">12</span>   <span style="color: rgba(0, 128, 0, 1)">/*</span>
<span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 128, 0, 1)">    label = gtk_label_new("label");
</span><span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(0, 128, 0, 1)">    gtk_container_add(GTK_CONTAINER(window),label);
</span><span style="color: rgba(0, 128, 128, 1)">15</span>   <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">16</span>   
<span style="color: rgba(0, 128, 128, 1)">17</span>   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> connect the destroy signal of the window to gtk_main_quit
</span><span style="color: rgba(0, 128, 128, 1)">18</span>   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> when the window is about to be destroyed we get a notification and
</span><span style="color: rgba(0, 128, 128, 1)">19</span>   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> stop the main GTK+ loop</span>
<span style="color: rgba(0, 128, 128, 1)">20</span>   
<span style="color: rgba(0, 128, 128, 1)">21</span>   g_signal_connect(window,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">destroy</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,G_CALLBACK(gtk_main_quit),NULL);
</span><span style="color: rgba(0, 128, 128, 1)">22</span>
<span style="color: rgba(0, 128, 128, 1)">23</span> <span style="color: rgba(0, 0, 0, 1)">    gtk_widget_show_all(window);
</span><span style="color: rgba(0, 128, 128, 1)">24</span>   
<span style="color: rgba(0, 128, 128, 1)">25</span>   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> start the main loop, and let it rest until the application is closed</span>
<span style="color: rgba(0, 128, 128, 1)">26</span> <span style="color: rgba(0, 0, 0, 1)">    gtk_main();
</span><span style="color: rgba(0, 128, 128, 1)">27</span>   
<span style="color: rgba(0, 128, 128, 1)">28</span>
<span style="color: rgba(0, 128, 128, 1)">29</span>   <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">30</span> }</span></pre>
</div>
<p><span style="font-size: 18px">编译</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 18px">gcc -o ggg gtk.c `pkg-config --cflags --libsgtk+-<span style="color: rgba(128, 0, 128, 1)">2.0</span>`</span></pre>
</div>
<p><span style="font-size: 18px">运行</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 18px">./ggg</span></pre>
</div>
<p><span style="font-size: 18px">运行时有警告,忽视即可。</span></p><br><br>
来源:https://www.cnblogs.com/ybqjymy/p/12964668.html
頁: [1]
查看完整版本: Ubuntu安装GTK+教程