代码之家  ›  专栏  ›  技术社区  ›  Amr Fawzy

我无法使用cmake和MinGW gcc编译器构建gtk gui

  •  1
  • Amr Fawzy  · 技术社区  · 7 年前

    我使用windows 8.1操作系统。MinGW和gtk路径都被添加到my path环境变量中。目前,我很难将代码与gtk库链接。

    这是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})
    

    @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 ..
    

    这是我试图构建的gui代码。

    #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无法将gtk库与我的gui代码链接,下面是我得到的错误:

    -- 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
    

    有人能帮我解决这个问题吗?

    1 回复  |  直到 7 年前
        1
  •  0
  •   liberforce    7 年前

    你的CMakelists看起来很奇怪,就像你试图用手做每件事一样。 看看这个老家伙 CMakeLists.txt : https://stackoverflow.com/a/5105523/518853

    它适用于GTK 2,但如果更换 2 3 .

    顺便说一句,GTK项目正在缓慢地从自动工具迁移到介子构建系统,因此您可能想看看GTK应用程序的介子构建文件是什么样子的:

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

    http://mesonbuild.com/Tutorial.html