为什么vcpkg安装的gtkmm无法正常使用?

就是gtkmm.h和gdkmm.h都可以include,但他们自己include的子头文件都报错。
关注者
1
被浏览
1,805
登录后你可以
不限量看优质回答 私信答主深度交流 精彩内容一键收藏

自问自答。

vcpkg似乎不支持包里多层次的include,要求包里的include全是相对于包的根目录的,从每个文件本身所在的目录去include也不行。恰巧gtkmm及其众多依赖都是违反这个规定的。vcpkg的github上有提过很多类似issue,但vcpkg官方似乎不是很想解决的亚子,比如说:

How do I use Gtkmm4.0 · Issue #18328 · microsoft/vcpkg (github.com)

[gtk:x64-windows] Extra directory depth required for GTK4 · Issue #18760 · microsoft/vcpkg (github.com)

我整理了下各issue下最终的解决方案,大概是:

  • 如果你用 gcc,就在之后加上 ` pkg-config gtkmm-4.0 `
  • 如果你用 cmake,就下载cmake下的pkg-config脚本工具然后使用
  • 如果你和我一样觉得上面这俩玩意(指gcc和cmake)很死马,那你得手动添加IncludePath

我应该属于初学者,对gcc和cmake不是很感冒。我的工作流就是Cherno式的Visual Studio+premake。上面提到的前两个解决方案对我不是很适用。

但手动IncludePath也很死马。上面的一个issue中有一个 post 也有老哥抱怨:

I've been believing vcpkg does not require VS setting to use a library... Even if it is unavoidable, the include path should not refer to the version number because the package name "gtk" does not refer to it.

Yeah, at least the vcpkg readme promises:

After this, you can now create a New non-CMake Project (or open an existing one). All installed libraries are immediately ready to be #include'd and used in your project without additional configuration.

(其实premake也有pkg-config工具,但不知道为什么我好像不是很能用的样子)

所以我得在premake5脚本中手动添加IncludePath:

workspace "MyWorkSpace"
    -- ...
local msys2 = os.getenv("MSYS_ROOT") --[[deprecated]]
local vcpkg = os.getenv("VCPKG_ROOT")
local msys2Include = msys2 .. "/mingw64/include"
local msys2Configs = msys2 .. "/mingw64/lib"
local msys2DLLs    = msys2 .. "/mingw64/bin"
local vcpkgInclude = vcpkg .. "/installed/x64-windows/include"
local vcpkgDLLs    = vcpkg .. "/installed/x64-windows/bin"
local vcpkgConfigs = vcpkg .. "/installed/x64-windows/lib"
project "MyProject"
    -- ...你其他的项目配置
    includedirs
        "%{prj.name}/src",
        vcpkgInclude,
        -- Local Copy of gtkmm
        -- "%{IncludeDir.gtkmm}",
        -- "%{IncludeDir.gtkmm}/gtkmm-4.0",
        -- "%{IncludeDir.gtkmm}/glibmm-2.68",
        -- "%{IncludeDir.gtkmm}/giomm-2.68",
        -- "%{IncludeDir.gtkmm}/glib-2.0",
        -- "%{IncludeDir.gtkmm}/gio-win32-2.0",
        -- "%{IncludeDir.gtkmm}/gtk-4.0",
        -- "%{IncludeDir.gtkmm}/gtk-3.0",
        -- "%{IncludeDir.gtkmm}/sigc++-3.0",
        -- "%{IncludeDir.gtkmm}/pangomm-2.48",
        -- "%{IncludeDir.gtkmm}/pango-1.0",
        -- "%{IncludeDir.gtkmm}/cairomm-1.16",
        -- "%{IncludeDir.gtkmm}/cairo",
        -- "%{IncludeDir.gtkmm}/harfbuzz",
        -- "%{IncludeDir.gtkmm}/gdk-pixbuf-2.0",
        -- "%{IncludeDir.gtkmm}/graphene-1.0",
        -- gtkmm from msys2
        -- gtkmm source and dependencies
        -- msys2Include,
        -- msys2Include .. "/gtkmm-4.0",
        -- msys2Include .. "/glibmm-2.68",
        -- msys2Include .. "/giomm-2.68",
        -- msys2Include .. "/glib-2.0",
        -- msys2Include .. "/gio-win32-2.0",
        -- msys2Include .. "/gtk-4.0",
        -- msys2Include .. "/gtk-3.0",
        -- msys2Include .. "/sigc++-3.0",
        -- msys2Include .. "/pangomm-2.48",
        -- msys2Include .. "/pango-1.0",
        -- msys2Include .. "/cairomm-1.16",
        -- msys2Include .. "/cairo",
        -- msys2Include .. "/harfbuzz",
        -- msys2Include .. "/gdk-pixbuf-2.0",
        -- msys2Include .. "/graphene-1.0",
        -- -- config files
        -- msys2Configs,
        -- msys2Configs .. "/gtkmm-4.0/include",
        -- msys2Configs .. "/glibmm-2.68/include",
        -- msys2Configs .. "/giomm-2.68/include",
        -- msys2Configs .. "/glib-2.0/include",
        -- msys2Configs .. "/gio-win32-2.0/include",
        -- msys2Configs .. "/gtk-4.0/include",
        -- msys2Configs .. "/gtk-3.0/include",
        -- msys2Configs .. "/sigc++-3.0/include",
        -- msys2Configs .. "/pangomm-2.48/include",
        -- msys2Configs .. "/pango-1.0/include",
        -- msys2Configs .. "/cairomm-1.16/include",
        -- msys2Configs .. "/cairo/include",
        -- msys2Configs .. "/harfbuzz/include",
        -- msys2Configs .. "/gdk-pixbuf-2.0/include",
        -- msys2Configs .. "/graphene-1.0/include",
        -- gtkmm from vcpkg --
        vcpkgInclude .. "/gtkmm-4.0",
        vcpkgInclude .. "/glibmm-2.68",
        vcpkgInclude .. "/giomm-2.68",
        vcpkgInclude .. "/glib-2.0",
        vcpkgInclude .. "/gio-win32-2.0",
        vcpkgInclude .. "/gtk-4.0",
        vcpkgInclude .. "/gtk-3.0",
        vcpkgInclude .. "/sigc++-3.0",
        vcpkgInclude .. "/pangomm-2.48",
        vcpkgInclude .. "/pango-1.0",
        vcpkgInclude .. "/cairomm-1.16",
        vcpkgInclude .. "/cairo",
        vcpkgInclude .. "/harfbuzz",
        vcpkgInclude .. "/gdk-pixbuf-2.0",
        vcpkgInclude .. "/graphene-1.0",
        --config files
        vcpkgConfigs .. "/gtkmm-4.0/include",
        vcpkgConfigs .. "/glibmm-2.68/include",
        vcpkgConfigs .. "/giomm-2.68/include",
        vcpkgConfigs .. "/glib-2.0/include",
        vcpkgConfigs .. "/gio-win32-2.0/include",
        vcpkgConfigs .. "/gtk-4.0/include",
        vcpkgConfigs .. "/gtk-3.0/include",
        vcpkgConfigs .. "/sigc++-3.0/include",
        vcpkgConfigs .. "/pangomm-2.48/include",
        vcpkgConfigs .. "/pango-1.0/include",
        vcpkgConfigs .. "/cairomm-1.16/include",
        vcpkgConfigs .. "/cairo/include",
        vcpkgConfigs .. "/harfbuzz/include",
        vcpkgConfigs .. "/gdk-pixbuf-2.0/include",
        vcpkgConfigs .. "/graphene-1.0/include",
        -- gtkmm_pkgconfig.cflags,
        -- pkgconfig.load('gtkmm-4.0').libs
        -- "D:/Applications/msys64/mingw64/include/glibmm-2.68",
        -- "D:/Applications/msys64/mingw64/include/giomm-2.68",