gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:21:50 30 4
gpt4 key购买 nike

我使用的是 Windows 8.1 操作系统。 MinGW 和 gtk 路径都添加到我的路径环境变量中。目前我无法将我的代码与 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

谁能帮我解决这个问题?

最佳答案

您的 CMakelists 看起来很奇怪,就像您在尝试手工完成所有事情一样。看看这个旧的 CMakeLists.txt:https://stackoverflow.com/a/5105523/518853

它适用于 GTK 2,但如果您将 2 替换为 3,则可以轻松调整。

顺便说一句,GTK 项目正慢慢从 autotools 迁移到 meson 构建系统,因此您可能想看看 GTK 应用程序的 meson 构建文件是什么样子的:

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

来源:http://mesonbuild.com/Tutorial.html

关于c - 我无法使用 cmake 和 MinGW gcc 编译器构建 gtk gui,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46135067/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com