gtk学习笔记01 helloworld

  1. 首先要有非常好用的clion工具

    • clion选择编译器,我这里选择了MinGW
      file->settings->ToolChains->点击+号(我这里自动识别了)->OK
      gtk学习笔记01 helloworld

    • 并且去配置cmake生成debugrelease

      • debug是默认有的,release我点加号的时候也自动识别了
        gtk学习笔记01 helloworld
    • 新建一个c++工程file -> new project -> c++ project并得到目录结构
      gtk学习笔记01 helloworld
      gtk学习笔记01 helloworld

  2. 搭建开发环境

    1. 下载GTK+ 3.6.4 Bundle for Windows

      网址http://www.tarnyko.net/dl/gtk.htm
      gtk学习笔记01 helloworld

  3. 解压后得到目录gtk+-3xxx

    • 我这里放到了H:\mysoft\clion\clionWorkSpace目录下
    • 下面有binincludelib之类的目录
      gtk学习笔记01 helloworld
    • bin目录下的dll是要放在编译好.exe文件的同级目录下,.exe文件才能运行(windows下优先搜索当前文件夹下的dll)
      gtk学习笔记01 helloworld
  4. includelib里的东西添加到cmakelists.txt(使用cmake构建)

    1. H:\mysoft\clion\clionWorkSpace部分需要自己修改哦
    2. include
      include_directories(
              H:/mysoft/clion/clionWorkSpace/gtk+364/include
              H:/mysoft/clion/clionWorkSpace/gtk+364/include/gtk-3.0
              H:/mysoft/clion/clionWorkSpace/gtk+364/include/cairo
              H:/mysoft/clion/clionWorkSpace/gtk+364/include/gdk
              H:/mysoft/clion/clionWorkSpace/gtk+364/include/glib-2.0
              H:/mysoft/clion/clionWorkSpace/gtk+364/include/pango-1.0
              H:/mysoft/clion/clionWorkSpace/gtk+364/include/atk-1.0
              H:/mysoft/clion/clionWorkSpace/gtk+364/include/gdk-pixbuf-2.0
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/glib-2.0/include
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/gtk-3.0/include
      )
      
      
    3. lib的所有.a文件
      link_libraries(
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libatk-1.0.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libcairo-gobject.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libcairo-script-interpreter.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libcairo.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libcroco-0.6.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libffi.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libfontconfig.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libfreetype.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libgailutil-3.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libgdk-3.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libgdk_pixbuf-2.0.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libgio-2.0.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libglib-2.0.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libgmodule-2.0.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libgobject-2.0.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libgthread-2.0.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libgtk-3.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libjasper.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libjpeg.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/liblzma.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libpango-1.0.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libpangocairo-1.0.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libpangoft2-1.0.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libpangowin32-1.0.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libpixman-1.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libpng.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libpng15.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/librsvg-2.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libtiff.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libtiffxx.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libxml2.dll.a
              H:/mysoft/clion/clionWorkSpace/gtk+364/lib/libz.dll.a
      )
      
    4. 使用Mingw编译好.exe后,双击运行时会弹出黑窗口控制台,在cmakelists.txt里添加以下代码可消除黑窗口
      # mingw消除黑窗口 C++工程
      set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -mwindows)
      
      # mingw消除黑窗口 C 工程
      # set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} -mwindows)
      
  5. 编写helloworld代码,把以下代码拷贝到main.cpp里面

        #include <gtk/gtk.h>
        #include <string.h>
    
        int main(int argc, char **argv) {
            GtkWidget *window;
            // 要先init
            gtk_init(&argc, &argv); 
            // 才能new
            window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    
            g_signal_connect(window, "delete_event", gtk_main_quit, NULL);
            gtk_widget_show_all(window);
            gtk_main();
            return 0;
        }
    
  6. 点击clion的小绿锤子编译(当然是可选的)得到.exe可执行文件

    • gtk学习笔记01 helloworld
  7. 把可执行文件和那些.dll放到同级目录下,双击运行即可(我还不知道如何把他们打包成单个可执行文件)

    • gtk学习笔记01 helloworld

    • gtk学习笔记01 helloworld

上一篇:c – fopen问题 – 打开的文件太多了


下一篇:【i.MX6ULL】驱动开发8——中断法检测按键