1

I using using windows 8.1 OS. Both MinGW and gtk paths are added to my path environment variable. current I have trouble to link my code with the gtk libraries.

here is the CMakeLists

# Set project
cmake_minimum_required(VERSION 3.0)
project(gtk-test C)

# Configure project paths
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
set(CMAKE_SOURCE_DIR ${CMAKE_SOURCE_DIR}/src)

# Find dependencies
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
link_directories(${GTK3_LIBRARY_DIRS})
add_compile_options(${GTK3_CFLAGS_OTHER})
set(LIBRARIES ${LIBRARIES} ${GTK3_LIBRARIES})

set(FLAGS "-I${GTK3_INCLUDE_DIRS}")
message(STATUS "Flags: ${FLAGS}")
string(REPLACE ";" " -I" FLAGS "${FLAGS}")
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} ${GTK3_FLAGS} ${FLAGS})

# Compile
add_executable(main ${PROJECT_SOURCE_DIR}/example.c)
target_link_libraries(main ${LIBRARIES})

here is the patch file I use to build.

@echo off

set CMAKE_PATH=%~dp0..\Tools\CMake\bin
set MAKE_PATH=%~dp0..\Tools\Make
set PKG_CONFIG_PATH=C:\gtk3\lib\pkgconfig
set PATH=C:\gtk3\bin;%PATH%
set PATH=%PATH%;%CMAKE_PATH%;%MAKE_PATH%
rd /s /q .\Builds
mkdir Builds
cd Builds
cmake .. -G "MinGW Makefiles"
cmake .. -G "MinGW Makefiles"
call mingw32-make all -j4

cd ..

here is the gui code I am trying to build.

#include <gtk/gtk.h>

int main(int argc, char *argv[])
{
GtkBuilder *gtkBuilder;
    GtkWidget *window;
    GError     *error = NULL;
    gtk_init(&argc, &argv);

    gtkBuilder = gtk_builder_new();
    if(!gtk_builder_add_from_file(gtkBuilder, "gui_config.glade", &error))
    {
        g_warning( "%s", error->message );
        g_free( error );
        return( 1 );
    }
    window = GTK_WIDGET(gtk_builder_get_object(gtkBuilder, "crc_main"));

     /* Connect signals */
    gtk_builder_connect_signals( gtkBuilder, NULL );

     /* Destroy builder, since we don't need it anymore */   
    g_object_unref(G_OBJECT(gtkBuilder));

    gtk_widget_show(window);
    gtk_main();

    return 0;
}

MinGW is failing to link gtk libraries with my gui code, here is the error I get:

-- The C compiler identification is GNU 6.3.0
-- Check for working C compiler: C:/MinGW/bin/gcc.exe
-- Check for working C compiler: C:/MinGW/bin/gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Found PkgConfig: C:/gtk3/bin/pkg-config.exe (found version "0.28")
-- Checking for module 'gtk+-3.0'
--   Found gtk+-3.0, version 3.6.4
-- Flags: -IC:/gtk3/include/gtk-3.0;C:/gtk3/include/cairo;C:/gtk3/include/pango-
1.0;C:/gtk3/include/atk-1.0;C:/gtk3/include/cairo;C:/gtk3/include/pixman-1;C:/gt
k3/include;C:/gtk3/include/freetype2;C:/gtk3/include;C:/gtk3/include/libpng15;C:
/gtk3/include/gdk-pixbuf-2.0;C:/gtk3/include/libpng15;C:/gtk3/include/glib-2.0;C
:/gtk3/lib/glib-2.0/include
-- Configuring done
-- Generating done
-- Build files have been written to: E:/crc_update/trunk/GUI/Builds
-- Flags: -IC:/gtk3/include/gtk-3.0;C:/gtk3/include/cairo;C:/gtk3/include/pango-
1.0;C:/gtk3/include/atk-1.0;C:/gtk3/include/cairo;C:/gtk3/include/pixman-1;C:/gt
k3/include;C:/gtk3/include/freetype2;C:/gtk3/include;C:/gtk3/include/libpng15;C:
/gtk3/include/gdk-pixbuf-2.0;C:/gtk3/include/libpng15;C:/gtk3/include/glib-2.0;C
:/gtk3/lib/glib-2.0/include
-- Configuring done
-- Generating done
-- Build files have been written to: E:/crc_update/trunk/GUI/Builds
Scanning dependencies of target main
[ 50%] Building C object CMakeFiles/main.dir/example.c.obj
[100%] Linking C executable ..\bin\main.exe
CMakeFiles\main.dir/objects.a(example.c.obj):example.c:(.text+0x3e): undefined r
eference to `gtk_init_abi_check'
CMakeFiles\main.dir/objects.a(example.c.obj):example.c:(.text+0x43): undefined r
eference to `gtk_builder_new'
CMakeFiles\main.dir/objects.a(example.c.obj):example.c:(.text+0x63): undefined r
eference to `gtk_builder_add_from_file'
CMakeFiles\main.dir/objects.a(example.c.obj):example.c:(.text+0x8e): undefined r
eference to `g_log'
CMakeFiles\main.dir/objects.a(example.c.obj):example.c:(.text+0x9a): undefined r
eference to `g_free'
CMakeFiles\main.dir/objects.a(example.c.obj):example.c:(.text+0xa6): undefined r
eference to `gtk_widget_get_type'
CMakeFiles\main.dir/objects.a(example.c.obj):example.c:(.text+0xbe): undefined r
eference to `gtk_builder_get_object'
CMakeFiles\main.dir/objects.a(example.c.obj):example.c:(.text+0xce): undefined r
eference to `g_type_check_instance_cast'
CMakeFiles\main.dir/objects.a(example.c.obj):example.c:(.text+0xe6): undefined r
eference to `gtk_builder_connect_signals'
CMakeFiles\main.dir/objects.a(example.c.obj):example.c:(.text+0x102): undefined
reference to `g_type_check_instance_cast'
CMakeFiles\main.dir/objects.a(example.c.obj):example.c:(.text+0x10a): undefined
reference to `g_object_unref'
CMakeFiles\main.dir/objects.a(example.c.obj):example.c:(.text+0x116): undefined
reference to `gtk_widget_show'
CMakeFiles\main.dir/objects.a(example.c.obj):example.c:(.text+0x11b): undefined
reference to `gtk_main'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\main.dir\build.make:95: recipe for target '../bin/main.exe' failed
mingw32-make[2]: *** [../bin/main.exe] Error 1
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/main.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/main.dir/all] Error 2
Makefile:82: recipe for target 'all' failed
mingw32-make: *** [all] Error 2

can any one help me with this issue?

Amr Fawzy
  • 21
  • 5
  • You are checking *compile flags*, but reason of "undefined reference" is absence of proper **library to link**, not a compile flags. BTW, for setting include directories there is CMake command `include_directories`; no need to convert them to `CMAKE_C_FLAGS` (which is done incorrectly, as `-I` option has been appended only to the first directory). Also, never set `CMAKE_SOURCE_DIR` - this is automatic CMake variable, which should always point to the root of the project. – Tsyvarev Sep 09 '17 at 22:47
  • I used include_directories of the gtk libraries but it gives the same result. is do you have any other thoughts why the library linking is failing? – Amr Fawzy Sep 10 '17 at 19:05
  • You may check content of `LIBRARIES` variable which you use. – Tsyvarev Sep 10 '17 at 20:04
  • done and they correct, actually you can see them in log of the compiler output up there. – Amr Fawzy Sep 10 '17 at 20:05
  • There is no **libraries** in the log you show, only *include directories*. – Tsyvarev Sep 10 '17 at 20:58
  • -- LIBRARIES: gtk-3gdk-3gdi32imm32shell32ole32pangocairo-1.0pangoft2-1.0freetype fontconfigpangowin32-1.0gdi32pango-1.0matk-1.0cairo-gobjectcairogdk_pixbuf-2.0gi o-2.0gobject-2.0glib-2.0intl – Amr Fawzy Sep 10 '17 at 21:18

1 Answers1

0

Your CMakelists looks weird, it's like you're trying to do everything by hand. Look at this old CMakeLists.txt: https://stackoverflow.com/a/5105523/518853

It's for GTK 2 but can easily adapted if you replace the 2 by 3.

By the way, the GTK project is slowly migrating from autotools to the meson build system, so you may wan to give a look at how a meson build file for a GTK app looks like:

project('tutorial', 'c')
gtkdep = dependency('gtk+-3.0')
executable('demo', 'main.c', dependencies : gtkdep)

Source: http://mesonbuild.com/Tutorial.html

liberforce
  • 11,189
  • 37
  • 48
  • It is not working for me. Still I have the linker errors that the Mingw can not find the libraries to link with. – Amr Fawzy Sep 15 '17 at 12:11
  • In your logs I see your GTK is installed in `C:/gtk`, and it's 3.6.4 which is really old. Why don't you just use MSYS2 to setup your MinGW environment and install the GTK library using MSYS2 `pacman` tool as it is clearly stated in the GTK website? Unless you have a very good reason to use an old GTK, that is the way you should go. Use the `pacman` package manager to install CMake too. – liberforce Sep 18 '17 at 09:05