GTK3程序不能在多个源文件中吗?

时间:2017-08-06 21:56:17

标签: gcc gtk3

我正在尝试在GTK3中创建一个包含多个c文件的C程序。

我正在使用的模型: how-to-split-a-c-program-into-multiple-files 还有这个 example-0.c

我的程序如下所示:

Functions.c(功能激活)

#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(头文件)

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

当我编译时,说:

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

并且未编译!

1 个答案:

答案 0 :(得分:1)

是的,GTK程序可以拆分成多个C文件。

您收到错误,因为您告诉编译器该函数仅在该单个C源文件中可见:

static void activate (GtkApplication* app, gpointer user_data)

如果要从其他源文件中使用该功能,则需要删除static关键字。 既可以在头文件中也可以在C文件中删除它。