无法在 arm64Apple Silicon上使用 GTK+3 构建 C11 应用程序

问题描述

我正在尝试使用 GTK+3 在 C 语言下创建一个基本应用程序。唯一的问题是我目前使用的是 M1(Apple Silicon)Mac。

我在 x86_64 接口上安装了带有 Homebrew 的 gtk+3,因为它还与 arm64 不兼容。

我将 CLion 与 CMake 3.19.2 一起使用:

cmake_minimum_required(VERSION 3.19.2)
project(vetplus_c C)
set(CMAKE_APPLE_SILICON_PROCESS,arm64)
set(CMAKE_C_STANDARD 11)

include_directories(.)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c11")

set(SOURCE_FILES main.c)
add_executable(gtk_test ${SOURCE_FILES})

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

target_link_libraries(gtk_test ${GTK3_LIBRARIES})

add_executable(vetplus_c
        main.c)

使用此代码

#include <stdio.h> 
#include <gtk/gtk.h>

static void on_activate (GtkApplication *app) {
    // Create a new window
    GtkWidget *window = gtk_application_window_new (app);
    // Create a new button
    GtkWidget *button = gtk_button_new_with_label ("Hello,World!");
    // When the button is clicked,close the window passed as an argument
    g_signal_connect_swapped (button,"clicked",G_CALLBACK (gtk_window_close),window);
    //gtk_window_set_child (GTK_WINDOW (window),button);
    gtk_window_present (window);
}

int main(int argc,char **argv)
{
    printf("ok");
    GtkApplication *app = gtk_application_new ("com.example.GtkApplication",G_APPLICATION_FLAGS_NONE);
    g_signal_connect (app,"activate",G_CALLBACK (on_activate),NULL);
    return g_application_run (G_APPLICATION (app),argc,argv);
    return 0;
}

我目前遇到的错误是这个:

Undefined symbols for architecture x86_64:
  "_g_application_get_type",referenced from:
      _main in main.c.o
  "_g_application_run",referenced from:
      _main in main.c.o
  "_g_signal_connect_data",referenced from:
      _main in main.c.o
      _on_activate in main.c.o
  "_g_type_check_instance_cast",referenced from:
      _main in main.c.o
  "_gtk_application_new",referenced from:
      _main in main.c.o
  "_gtk_application_window_new",referenced from:
      _on_activate in main.c.o
  "_gtk_button_new_with_label",referenced from:
      _on_activate in main.c.o
  "_gtk_window_close",referenced from:
      _on_activate in main.c.o
  "_gtk_window_present",referenced from:
      _on_activate in main.c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command Failed with exit code 1 (use -v to see invocation)

如果有人能解决这个问题,我将不胜感激。

提前致谢。

解决方法

在多种搜索方式后修复。

我需要将 CMake 文件编辑为:

cmake_minimum_required(VERSION 3.19.2)
project(vetplus_c C)
set(CMAKE_APPLE_SILICON_PROCESS,arm64)
set(CMAKE_OSX_ARCHITECTURES,arm64)
set(CMAKE_C_STANDARD 11)

include_directories(.)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c11")

set(SOURCE_FILES main.c)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
# Setup CMake to use GTK+,tell the compiler where to look for headers
# and to the linker where to look for libraries
include_directories(${GTK3_INCLUDE_DIRS})
link_directories(${GTK3_LIBRARY_DIRS})
# Add other flags to the compiler
add_definitions(${GTK3_CFLAGS_OTHER})
# Add an executable compiled from hello.c
add_executable(vetplus_c main.c)
# Link the target to the GTK+ libraries
target_link_libraries(vetplus_c ${GTK3_LIBRARIES})