The CMake installed from pacman within MSYS2 seems to prefix library and header/include paths with C:/, but the actual path format needed is a prefix of /c/. I've used a regex replace as a kludge but it's not a very elegant solution and I'm worried this will break things like MingW.
Just for reference the library path being obtianed by CMake is:
C:/msys64/mingw64/include/gtk-3.0;C:/msys64/mingw64/include/cairo;C:/msys64/mingw64/include;C:/msys64/mingw64/include/pango-1.0;C:/msys64/mingw64/include/fribidi;C:/msys64/mingw64/include;C:/msys64/mingw64/include/atk-1.0;C:/msys64/mingw64/include/cairo;C:/msys64/mingw64/include/pixman-1;C:/msys64/mingw64/include;C:/msys64/mingw64/include/freetype2;C:/msys64/mingw64/include;C:/msys64/mingw64/include/harfbuzz;C:/msys64/mingw64/include/libpng16;C:/msys64/mingw64/include/gdk-pixbuf-2.0;C:/msys64/mingw64/include/libpng16;C:/msys64/mingw64/include;C:/msys64/mingw64/lib/libffi-3.2.1/include;C:/msys64/mingw64/include/glib-2.0;C:/msys64/mingw64/lib/glib-2.0/include;C:/msys64/mingw64/include
and it needs to be:
/c//msys64/mingw64/include/gtk-3.0;/c//msys64/mingw64/include/cairo;/c//msys64/mingw64/include;/c//msys64/mingw64/include/pango-1.0;/c//msys64/mingw64/include/fribidi;/c//msys64/mingw64/include;/c//msys64/mingw64/include/atk-1.0;/c//msys64/mingw64/include/cairo;/c//msys64/mingw64/include/pixman-1;/c//msys64/mingw64/include;/c//msys64/mingw64/include/freetype2;/c//msys64/mingw64/include;/c//msys64/mingw64/include/harfbuzz;/c//msys64/mingw64/include/libpng16;/c//msys64/mingw64/include/gdk-pixbuf-2.0;/c//msys64/mingw64/include/libpng16;/c//msys64/mingw64/include;/c//msys64/mingw64/lib/libffi-3.2.1/include;/c//msys64/mingw64/include/glib-2.0;/c//msys64/mingw64/lib/glib-2.0/include;/c//msys64/mingw64/include
I've been digging for a solution and saw that CMake for Windows (not the CMake installed from within MSYS2) has a MSYS specific makefile generator, but there must be some simple solution to use the CMake available within MSYS2. I can't imagine I'm the only one who's encountered this issue. Does anyone know of a clean solution?
UPDATE: Here is the CMakeLists.txt without the regex replace hack. This fails with the error
C:/msys64/home/username/sample/src/main.cpp:1:10 fatal error: gtk/gtk.h: No such file or directory
#include <gtk/gtk.h>
compilation terminated.
during make.
cmake_minimum_required(VERSION 3.12)
project(sample CXX)
# Find GTK+ headers/libs with PkgConfig
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
include_directories(${GTK3_INCLUDE_DIRS})
link_directories(${GTK3_LIBRARY_DIRS})
add_definitions(${GTK3_CFLAGS_OTHER})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib/natives)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib/natives)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
add_executable(sample src/main.cpp)
target_link_libraries(sample ${GTK3_LIBRARIES})
For main.cpp just use the default GTK+ hello world
#include <gtk/gtk.h>
int
main (int argc,
char *argv[])
{
GtkWidget *window;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_widget_show (window);
gtk_main ();
return 0;
}
To make compilation work, I'm using the following CMake kludge:
cmake_minimum_required(VERSION 3.12)
project(sample CXX)
# Find GTK+ headers/libs with PkgConfig
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
# Generated paths starting with "C:" need to be converted to /c/ to work with MSYS2
# TODO remove this or do it some way better at some point in the future
if(MSYS OR MINGW)
string(REGEX REPLACE "C:" "/c/" GTK3_INCLUDE_DIRS "${GTK3_INCLUDE_DIRS}")
endif(MSYS OR MINGW)
include_directories(${GTK3_INCLUDE_DIRS})
link_directories(${GTK3_LIBRARY_DIRS})
add_definitions(${GTK3_CFLAGS_OTHER})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib/natives)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib/natives)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
add_executable(sample src/main.cpp)
target_link_libraries(sample ${GTK3_LIBRARIES})
Note the REGEX REPLACE.
Another thing to note is I'm only using tools, such as cmake, and libraries/headers/etc. installed from pacman within MSYS2 and I'd prefer to keep it that way because I'll be rolling something out based on this as a Ruby native extension and the Windows SDK for that is MSYS2 based. At the same time, I'm fairly sure my kludge will break MingW based builds (if I even need to worry about that?) and it's not very flexible.
The problem has to do with how MingW packages are/were ported to MSYS2. The paths are in "Windows" format for backwards compatibility and to preserve compatibility with systems like Visual Studio. The only answer is to do regex replace like I've pointed out above or to just recompile the libraries in MSYS2 like I've now chosen to do.
As a general guideline, packages available in MSYS2 without mingw prefixes in the package names don't have this "backward compatibility" issue and, as far as I know, don't require such a workaround.