2017-08-06 20 views
1

我想用GTK3中的多個c文件製作一個C程序。GTK3程序不能在多個源文件中?

至於車型我使用: how-to-split-a-c-program-into-multiple-filesexample-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); 
} 

功能.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 

和未編譯!

+0

我的問題是通過刪除@Gerhardh建議的「靜態」來解決的。 –

+0

如果答案解決了您的問題,您可能會考慮將該答案標記爲已接受。 – Gerhardh

+0

我很樂意。但是,如何將其標記爲已接受? –

回答

1

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

static void activate (GtkApplication* app, gpointer user_data) 

如果你想使用該功能從其他來源的文件,你需要刪除:

你,因爲你是在告訴你的編譯器的功能僅僅是單一的C源文件中可見得到一個錯誤static關鍵字。 無論是在頭文件還是在C文件中,您都需要將其刪除。