Search code examples
ccmakegtkapple-silicon

Can't build C11 application with GTK+3 on arm64 (Apple Silicon)


I'm trying to create a basic application under the C language with GTK+3. The only problem is that I'm currently on an M1 (Apple Silicon) Mac.

I installed gtk+3 with Homebrew on the x86_64 interface since it is not yet compatible with arm64.

I use CLion with 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)

With this code :

#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;
}

The error I am currently encountering is this one :

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)

If anyone would have a solution to this problem, I would be very grateful.

Thank you in advance.


Solution

  • Fixed after multiples way of search.

    I needed to edit the CMake file to this :

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