1

I am trying to make a C-program with multiple c-files in GTK3.

As models I am using: how-to-split-a-c-program-into-multiple-files and this example-0.c

My program looks like this:

Functions.c (with the function activate)

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

static void activate (GtkApplication* app, gpointer user_data)
{
  GtkWidget *window;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Window");
  gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
  gtk_widget_show_all (window);
}

Function.h (the header file)

#include <gtk/gtk.h>
#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED

 static void activate (GtkApplication* app,  gpointer user_data);
#endif

Main.c

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

int main(int    argc,      char **argv)
{
    GtkApplication *app;
    int status;

    app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
    g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
    status = g_application_run (G_APPLICATION (app), argc, argv);
    g_object_unref (app);
    return status;
}

When I compile, is says:

gcc -Wall `pkg-config --cflags gtk+-3.0` Functions.c  Main.c `pkg-config --libs gtk+-3.0`
Functions.c:7:13: warning: ‘activate’ defined but not used [-Wunused-function]
 static void activate (GtkApplication* app, gpointer user_data)
             ^
In file included from Main.c:4:0:
Functions.h:7:14: warning: ‘activate’ used but never defined
  static void activate (GtkApplication* app,  gpointer user_data);
              ^
/tmp/ccWzazr0.o: I funktionen "main":
Main.c:(.text+0x38): undefined reference to `activate'
collect2: error: ld returned 1 exit status

and is not compiled!

Store Nord
  • 13
  • 4

1 Answers1

1

Yes, GTK programs can be split into multiple C files.

You get an error because you are telling your compiler that the function is only visible within that single C source file:

static void activate (GtkApplication* app, gpointer user_data)

If you want to use that function from another source file, you need to remove static keyword. Both in the header file as well in the C file you need to remove it.

Gerhardh
  • 11,688
  • 4
  • 17
  • 39