版本说明
OS版本:Microsoft Windows [版本 10.0.18363.778]
C:B版本:CodeBlock20.03自带gcc(x86_64-w64-mingw32/8.1.0)
GTK+3版本:本文下载tarnyko提供64-bit的GTK+3: gtk+ -bundle_3.6.4-20130513_win64.zip (31.0 Mb)
解压GTK
1 解压,保证目录名里没有空格没有汉字。
安装C:B
1 C:B 提示安装即可。本文安装目录D:\programFile\CodeBlocks,其中gcc安装在D:\programFile\CodeBlocks\MinGW\bin下。
配置
1 “计算机”–>“属性”在侧边栏选择“高级系统设置”->在弹出的窗口选择"高级"标签页"–>“环境变量” 在“系统变量”里, 按“新建” 名称GTKDIR,值为解压目录。
2 Path添加%GTKDIR%\bin,注意路径间要英文输入法下分号“;”隔开 。
3 Path继续添加D:\programFile\CodeBlocks\MinGW\bin。
按“确定”。

4 打开 命令行工具 CMD
敲入pkg-config --cflags gtk+-3.0 按回车
确定Path里GTK路径配置正确。
敲入gcc 按回车。

确定Path里gcc路径配置正确。
配置GTK模块:
pango-querymodules > %GTKDIR%\etc\pango\pango.modules
gdk-pixbuf-query-loaders > %GTKDIR%\lib\gdk-pixbuf-2.0\2.10.0\loaders.cache
gtk-query-immodules-3.0 > %GTKDIR%\lib\gtk-3.0\3.0.0\immodules.cache
敲入 ”gtk3-demo“,出现下图则GTK配置成功。

**C:B配置GTK+3项目模板
**
在新建项目对话框中,点击GTK+ project图标,在图标上右击,选择Edit this script,

将下方代码覆盖原脚本即可
////////////////////////////////////////////////////////////////////////////////
//
// GTK project wizard
//
////////////////////////////////////////////////////////////////////////////////
// globals
GtkPathDefault <- _T("$(#gtk)");
GtkPathDefaultInc <- _T("$(#gtk.include)");
GtkPathDefaultLib <- _T("$(#gtk.lib)");
GtkPath <- _T("");
// GtkVersion <- 1; // 0 - GTK+, 1 - GTK+-2.0
function BeginWizard()
{
local intro_msg = _T("Welcome to the new GTK project wizard!\n" +
"This wizard will guide you to create a new GTK project\n" +
"using the GTK cross-platform GUI toolkit\n\n" +
"When you're ready to proceed, please click \"Next\"...");
local gtkpath_msg = _T("Please select the location of GTK on your computer.\n" +
"This is the top-level folder where GTK was installed.\n" +
"To help you, this folder must contain the subfolders\n" +
"\"include\" and \"lib\".");
Wizard.AddInfoPage(_T("GtkIntro"), intro_msg);
Wizard.AddProjectPathPage();
if (PLATFORM == PLATFORM_MSW)
{
Wizard.AddGenericSelectPathPage(_T("GtkPath"), gtkpath_msg, _T("GTK's location:"), GtkPathDefault);
}
/*else
{
Wizard.AddGenericSingleChoiceListPage(_T("GtkVersionPage"), _T("Please select the GTK+ version you want to use."), _T("GTK+ 1.x;GTK+ 2.x"), GtkVersion); // select GTK+ version
}*/
Wizard.AddCompilerPage(_T(""), _T("gcc*"), true, true);
}
////////////////////////////////////////////////////////////////////////////////
// Gtk's path page
////////////////////////////////////////////////////////////////////////////////
function OnLeave_GtkPath(fwd)
{
if (fwd)
{
local dir = Wizard.GetTextControlValue(_T("txtFolder")); // txtFolder is the text control in GenericSelectPathPage
local dir_nomacro = VerifyDirectory(dir);
if (dir_nomacro.IsEmpty())
return false;
// verify include dependencies
local dir_nomacro_inc = GetCompilerIncludeDir(dir, GtkPathDefault, GtkPathDefaultInc);
if (dir_nomacro_inc.IsEmpty())
return false;
if (!VerifyFile(dir_nomacro_inc + wxFILE_SEP_PATH + _T("gtk-3.0") + wxFILE_SEP_PATH +_T("gtk"), _T("gtk.h"), _T("GTK's include")))
return false;
// verify library dependencies
local dir_nomacro_lib = GetCompilerLibDir(dir, GtkPathDefault, GtkPathDefaultLib);
if (dir_nomacro_lib.IsEmpty())
return false;
/* if (!VerifyLibFile(dir_nomacro_lib, _T("gtk-win32-2.0"), _T("GTK's")))
return false;*/
if (!VerifyLibFile(dir_nomacro_lib, _T("gtk-win32-3.0"), _T("GTK's")))
return false;
GtkPath = dir; // Remember the original selection.
local is_macro = _T("");
// try to resolve the include directory as macro
is_macro = GetCompilerIncludeMacro(dir, GtkPathDefault, GtkPathDefaultInc);
if (is_macro.IsEmpty())
{
// not possible -> use the real inc path we had computed instead
GtkPathDefaultInc = dir_nomacro_inc;
}
// try to resolve the library directory as macro
is_macro = GetCompilerLibMacro(dir, GtkPathDefault, GtkPathDefaultLib);
if (is_macro.IsEmpty())
{
// not possible -> use the real lib path we had computed instead
GtkPathDefaultLib = dir_nomacro_lib;
}
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
// Gtk's version page (For *nix users)
////////////////////////////////////////////////////////////////////////////////
function OnLeave_GtkVersionPage(fwd)
{
if (fwd)
{
GtkVersion = Wizard.GetListboxSelection(_T("GenericChoiceList"));
}
return true;
}
// return the files this project contains
function GetFilesDir()
{
return _T("gtk/files");
}
// setup the already created project
function SetupProject(project)
{
if (PLATFORM == PLATFORM_MSW)
{
project.AddIncludeDir(GtkPathDefaultInc);
project.AddIncludeDir(GtkPathDefaultInc + wxFILE_SEP_PATH + _T("gtk-3.0"));
project.AddIncludeDir(GtkPathDefaultInc + wxFILE_SEP_PATH + _T("cairo"));
project.AddIncludeDir(GtkPathDefaultInc + wxFILE_SEP_PATH + _T("gdk"));
project.AddIncludeDir(GtkPathDefaultInc + wxFILE_SEP_PATH + _T("glib-2.0"));
// Notice GtkPathDefault*Lib* at some positions. This is correct as of 2.8.20
project.AddIncludeDir(GtkPathDefaultLib + wxFILE_SEP_PATH + _T("glib-2.0") + wxFILE_SEP_PATH + _T("include"));
project.AddIncludeDir(GtkPathDefaultInc + wxFILE_SEP_PATH + _T("pango-1.0"));
project.AddIncludeDir(GtkPathDefaultLib + wxFILE_SEP_PATH + _T("gtk-3.0") + wxFILE_SEP_PATH + _T("include"));
project.AddIncludeDir(GtkPathDefaultInc + wxFILE_SEP_PATH + _T("atk-1.0"));
if ( IO.DirectoryExists(GtkPathDefaultInc + wxFILE_SEP_PATH + _T("gdk-pixbuf-2.0")) )
project.AddIncludeDir(GtkPathDefaultInc + wxFILE_SEP_PATH + _T("gdk-pixbuf-2.0"));
project.AddLibDir(GtkPathDefaultLib);
// add link libraries
/* project.AddLinkLib(_T("gtk-win32-2.0"));
generate with pkg-config commande on MS Windows. the command was pkg-config gtk+-3.0 --libs and current directory was
gtk3.0/bin
-lgtk-3 -lgdk-3 -lgdi32 -limm32 -lshell32 -lole32 -Wl,-luuid -lpangocairo-1.0
-lpangoft2-1.0 -lfreetype -lfontconfig -lpangowin32-1.0 -lgdi32 -lpango-1.0 -lm -latk-1.0 -lcairo-gobject -lcairo
-lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lintl
project.AddLinkLib(_T("gtk-3"));
project.AddLinkLib(_T("gdk-3"));
project.AddLinkLib(_T("imm32"));
project.AddLinkLib(_T("shell32"));
project.AddLinkLib(_T("ole32"));
project.AddLinkLib(_T("Wl"));
project.AddLinkLib(_T("uuid"));
project.AddLinkLib(_T("pangocairo-1.0"));
project.AddLinkLib(_T("pangoft2-1.0"));
project.AddLinkLib(_T("freetype"));
project.AddLinkLib(_T("fontconfig"));
project.AddLinkLib(_T("pangowin32-1.0"));
project.AddLinkLib(_T("gdi32"));
project.AddLinkLib(_T("m"));
project.AddLinkLib(_T("atk-1.0"));
project.AddLinkLib(_T("cairo-gobject"));
project.AddLinkLib(_T("cairo"));
project.AddLinkLib(_T("gdk_pixbuf-2.0"));
project.AddLinkLib(_T("gio-2.0"));
project.AddLinkLib(_T("gobject-2.0"));
project.AddLinkLib(_T("glib-2.0"));
project.AddLinkLib(_T("intl"));
//project.AddLinkLib(_T("glib-2.0"));*/
project.AddLibDir(GtkPathDefaultLib);
// add link libraries
project.AddLinkLib(_T("gtk-win32-3.0"));
project.AddLinkLib(_T("gobject-2.0"));
project.AddLinkLib(_T("glib-2.0"));
// Notice: there are more libs required as the app gets more complex, e.g.:
// pangocairo-1.0.lib, pangocairo-1.0.lib, libatk-1.0.dll.a,
// gdk_pixbuf-2.0.lib, gdk-win32-2.0.lib, pango-1.0.lib,
// gmodule-2.0.lib, gthread-2.0.lib, cairo.lib,
// pangoft2-1.0.lib (...)
project.AddCompilerOption(_T("-mms-bitfields"));
}
else
{
/*if (GtkVersion == 1)
{*/
project.AddCompilerOption(_T("`pkg-config gtk+-3.0 --cflags`"));
project.AddLinkerOption(_T("`pkg-config gtk+-3.0 --libs`"));
/*}
else
{
project.AddCompilerOption(_T("`pkg-config gtk+ --cflags`"));
project.AddLinkerOption(_T("`pkg-config gtk+ --libs`"));
}*/
}
// enable compiler warnings (project-wide)
WarningsOn(project, Wizard.GetCompilerID());
// Debug
local target = project.GetBuildTarget(Wizard.GetDebugName());
if (!IsNull(target))
{
target.SetTargetType(ttConsoleOnly); // ttConsoleOnly: console for debugging
target.SetOutputFilename(Wizard.GetDebugOutputDir() + Wizard.GetProjectName() + DOT_EXT_EXECUTABLE);
// enable generation of debugging symbols for target
DebugSymbolsOn(target, Wizard.GetCompilerID());
}
// Release
target = project.GetBuildTarget(Wizard.GetReleaseName());
if (!IsNull(target))
{
target.SetTargetType(ttExecutable); // ttExecutable: no console
target.SetOutputFilename(Wizard.GetReleaseOutputDir() + Wizard.GetProjectName() + DOT_EXT_EXECUTABLE);
// enable optimizations for target
OptimizationsOn(target, Wizard.GetCompilerID());
}
return true;
}
完成后可点击GTK模板新建一个GTK+3项目,

内含main.c文件

代码如下
////////////////////////////////////////////////////////////////////////////////
//
// GTK project wizard
//
////////////////////////////////////////////////////////////////////////////////
// globals
GtkPathDefault <- _T("$(#gtk)");
GtkPathDefaultInc <- _T("$(#gtk.include)");
GtkPathDefaultLib <- _T("$(#gtk.lib)");
GtkPath <- _T("");
// GtkVersion <- 1; // 0 - GTK+, 1 - GTK+-2.0
function BeginWizard()
{
local intro_msg = _T("Welcome to the new GTK project wizard!\n" +
"This wizard will guide you to create a new GTK project\n" +
"using the GTK cross-platform GUI toolkit\n\n" +
"When you're ready to proceed, please click \"Next\"...");
local gtkpath_msg = _T("Please select the location of GTK on your computer.\n" +
"This is the top-level folder where GTK was installed.\n" +
"To help you, this folder must contain the subfolders\n" +
"\"include\" and \"lib\".");
Wizard.AddInfoPage(_T("GtkIntro"), intro_msg);
Wizard.AddProjectPathPage();
if (PLATFORM == PLATFORM_MSW)
{
Wizard.AddGenericSelectPathPage(_T("GtkPath"), gtkpath_msg, _T("GTK's location:"), GtkPathDefault);
}
/*else
{
Wizard.AddGenericSingleChoiceListPage(_T("GtkVersionPage"), _T("Please select the GTK+ version you want to use."), _T("GTK+ 1.x;GTK+ 2.x"), GtkVersion); // select GTK+ version
}*/
Wizard.AddCompilerPage(_T(""), _T("gcc*"), true, true);
}
////////////////////////////////////////////////////////////////////////////////
// Gtk's path page
////////////////////////////////////////////////////////////////////////////////
function OnLeave_GtkPath(fwd)
{
if (fwd)
{
local dir = Wizard.GetTextControlValue(_T("txtFolder")); // txtFolder is the text control in GenericSelectPathPage
local dir_nomacro = VerifyDirectory(dir);
if (dir_nomacro.IsEmpty())
return false;
// verify include dependencies
local dir_nomacro_inc = GetCompilerIncludeDir(dir, GtkPathDefault, GtkPathDefaultInc);
if (dir_nomacro_inc.IsEmpty())
return false;
if (!VerifyFile(dir_nomacro_inc + wxFILE_SEP_PATH + _T("gtk-3.0") + wxFILE_SEP_PATH +_T("gtk"), _T("gtk.h"), _T("GTK's include")))
return false;
// verify library dependencies
local dir_nomacro_lib = GetCompilerLibDir(dir, GtkPathDefault, GtkPathDefaultLib);
if (dir_nomacro_lib.IsEmpty())
return false;
/* if (!VerifyLibFile(dir_nomacro_lib, _T("gtk-win32-2.0"), _T("GTK's")))
return false;*/
if (!VerifyLibFile(dir_nomacro_lib, _T("gtk-win32-3.0"), _T("GTK's")))
return false;
GtkPath = dir; // Remember the original selection.
local is_macro = _T("");
// try to resolve the include directory as macro
is_macro = GetCompilerIncludeMacro(dir, GtkPathDefault, GtkPathDefaultInc);
if (is_macro.IsEmpty())
{
// not possible -> use the real inc path we had computed instead
GtkPathDefaultInc = dir_nomacro_inc;
}
// try to resolve the library directory as macro
is_macro = GetCompilerLibMacro(dir, GtkPathDefault, GtkPathDefaultLib);
if (is_macro.IsEmpty())
{
// not possible -> use the real lib path we had computed instead
GtkPathDefaultLib = dir_nomacro_lib;
}
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
// Gtk's version page (For *nix users)
////////////////////////////////////////////////////////////////////////////////
function OnLeave_GtkVersionPage(fwd)
{
if (fwd)
{
GtkVersion = Wizard.GetListboxSelection(_T("GenericChoiceList"));
}
return true;
}
// return the files this project contains
function GetFilesDir()
{
return _T("gtk/files");
}
// setup the already created project
function SetupProject(project)
{
if (PLATFORM == PLATFORM_MSW)
{
project.AddIncludeDir(GtkPathDefaultInc);
project.AddIncludeDir(GtkPathDefaultInc + wxFILE_SEP_PATH + _T("gtk-3.0"));
project.AddIncludeDir(GtkPathDefaultInc + wxFILE_SEP_PATH + _T("cairo"));
project.AddIncludeDir(GtkPathDefaultInc + wxFILE_SEP_PATH + _T("gdk"));
project.AddIncludeDir(GtkPathDefaultInc + wxFILE_SEP_PATH + _T("glib-2.0"));
// Notice GtkPathDefault*Lib* at some positions. This is correct as of 2.8.20
project.AddIncludeDir(GtkPathDefaultLib + wxFILE_SEP_PATH + _T("glib-2.0") + wxFILE_SEP_PATH + _T("include"));
project.AddIncludeDir(GtkPathDefaultInc + wxFILE_SEP_PATH + _T("pango-1.0"));
project.AddIncludeDir(GtkPathDefaultLib + wxFILE_SEP_PATH + _T("gtk-3.0") + wxFILE_SEP_PATH + _T("include"));
project.AddIncludeDir(GtkPathDefaultInc + wxFILE_SEP_PATH + _T("atk-1.0"));
if ( IO.DirectoryExists(GtkPathDefaultInc + wxFILE_SEP_PATH + _T("gdk-pixbuf-2.0")) )
project.AddIncludeDir(GtkPathDefaultInc + wxFILE_SEP_PATH + _T("gdk-pixbuf-2.0"));
project.AddLibDir(GtkPathDefaultLib);
// add link libraries
/* project.AddLinkLib(_T("gtk-win32-2.0"));
generate with pkg-config commande on MS Windows. the command was pkg-config gtk+-3.0 --libs and current directory was
gtk3.0/bin
-lgtk-3 -lgdk-3 -lgdi32 -limm32 -lshell32 -lole32 -Wl,-luuid -lpangocairo-1.0
-lpangoft2-1.0 -lfreetype -lfontconfig -lpangowin32-1.0 -lgdi32 -lpango-1.0 -lm -latk-1.0 -lcairo-gobject -lcairo
-lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lintl
project.AddLinkLib(_T("gtk-3"));
project.AddLinkLib(_T("gdk-3"));
project.AddLinkLib(_T("imm32"));
project.AddLinkLib(_T("shell32"));
project.AddLinkLib(_T("ole32"));
project.AddLinkLib(_T("Wl"));
project.AddLinkLib(_T("uuid"));
project.AddLinkLib(_T("pangocairo-1.0"));
project.AddLinkLib(_T("pangoft2-1.0"));
project.AddLinkLib(_T("freetype"));
project.AddLinkLib(_T("fontconfig"));
project.AddLinkLib(_T("pangowin32-1.0"));
project.AddLinkLib(_T("gdi32"));
project.AddLinkLib(_T("m"));
project.AddLinkLib(_T("atk-1.0"));
project.AddLinkLib(_T("cairo-gobject"));
project.AddLinkLib(_T("cairo"));
project.AddLinkLib(_T("gdk_pixbuf-2.0"));
project.AddLinkLib(_T("gio-2.0"));
project.AddLinkLib(_T("gobject-2.0"));
project.AddLinkLib(_T("glib-2.0"));
project.AddLinkLib(_T("intl"));
//project.AddLinkLib(_T("glib-2.0"));*/
project.AddLibDir(GtkPathDefaultLib);
// add link libraries
project.AddLinkLib(_T("gtk-win32-3.0"));
project.AddLinkLib(_T("gobject-2.0"));
project.AddLinkLib(_T("glib-2.0"));
// Notice: there are more libs required as the app gets more complex, e.g.:
// pangocairo-1.0.lib, pangocairo-1.0.lib, libatk-1.0.dll.a,
// gdk_pixbuf-2.0.lib, gdk-win32-2.0.lib, pango-1.0.lib,
// gmodule-2.0.lib, gthread-2.0.lib, cairo.lib,
// pangoft2-1.0.lib (...)
project.AddCompilerOption(_T("-mms-bitfields"));
}
else
{
/*if (GtkVersion == 1)
{*/
project.AddCompilerOption(_T("`pkg-config gtk+-3.0 --cflags`"));
project.AddLinkerOption(_T("`pkg-config gtk+-3.0 --libs`"));
/*}
else
{
project.AddCompilerOption(_T("`pkg-config gtk+ --cflags`"));
project.AddLinkerOption(_T("`pkg-config gtk+ --libs`"));
}*/
}
// enable compiler warnings (project-wide)
WarningsOn(project, Wizard.GetCompilerID());
// Debug
local target = project.GetBuildTarget(Wizard.GetDebugName());
if (!IsNull(target))
{
target.SetTargetType(ttConsoleOnly); // ttConsoleOnly: console for debugging
target.SetOutputFilename(Wizard.GetDebugOutputDir() + Wizard.GetProjectName() + DOT_EXT_EXECUTABLE);
// enable generation of debugging symbols for target
DebugSymbolsOn(target, Wizard.GetCompilerID());
}
// Release
target = project.GetBuildTarget(Wizard.GetReleaseName());
if (!IsNull(target))
{
target.SetTargetType(ttExecutable); // ttExecutable: no console
target.SetOutputFilename(Wizard.GetReleaseOutputDir() + Wizard.GetProjectName() + DOT_EXT_EXECUTABLE);
// enable optimizations for target
OptimizationsOn(target, Wizard.GetCompilerID());
}
return true;
}
点击 build and run

把 %GTKDIR%\bin 下所有目录都拷到项目目录/bin/debug/下面,该目录有个生成的helloGTK3.exe 。
再次点击build and run ,出现

大功告成。
注意
1
main.c|34|warning: ‘gtk_vbox_new’ is deprecated: Use ‘gtk_box_new’ instead [-Wdeprecated-declarations]|
把 gtk_vbox_new 换成 gtk_box_new’,消除告警。
2 显示汉字乱码,把字符集改成 utf-8

泪崩。。

总结
学着网上资料 搞完了 ,学习挺不容易,但 GTK+ 3.6.4 版本太低。。。
本文详细记录了在Windows 10系统中安装和配置GTK3及C:B(CodeBlocks)的过程,包括解压GTK,安装C:B,设置环境变量,配置GTK模块,以及解决配置过程中遇到的警告和汉字乱码问题。通过这些步骤,成功创建并运行了GTK+3项目。

765

被折叠的 条评论
为什么被折叠?



