2011-10-20 6 views
1

私はobjective-cとmakefilesの新機能ですが、現在makeを使ってコンパイルするには、objective-cとGtk + "hello world"を取得しようとしています。objective-cとgtkのmakefileを作成する+

# Suffixes 

.SUFFIXES: .o .m 
.m.o: 
    $(CC) -c $(CFLAGS) $< 

# Macros 
CC = gcc 
CFLAGS = -g 
GTKFLAGS= `pkg-config --cflags --libs gtk+-2.0` 
LIBS = -lobjc 
SRC = main.m MainWindow.m 
OBJ = main.o MainWindow.o 
PROG = gnulog514 

# Explicit rule 
all: hist 

hist: $(OBJ) 
    $(CC) $(CFLAGS) -o main $(OBJ) $(GTKFLAGS) $(LIBS) 

# Implicit rules 
MainWindow.o: MainWindow.h MainWindow.m 

を次のように メークコードがあると私はメイクの後に次のような出力が得られます。

gcc -c -g main.m 
In file included from main.m:1:0: 
MainWindow.h:1:20: fatal error: gtk/gtk.h: No such file or directory 
compilation terminated. 
make: *** [main.o] Error 1 

他にも必要なものがあります。

UPDATE:

$ gcc `pkg-config --cflags --libs gtk+2.0` -lgnustep-base -fconstant-string-class=NSConstantString -o "./myprogram" $(find . -name '*.m') -I /usr/include/GNUstep/ -L /usr/lib/GNUstep/ -std=c99 -O3 

コマンドを発行するとき、私は(GTK + -2.0にエラーGTK + 2.0を手に入れた)私は、次を得る 、助けるかもしれない何かを持っています出力

Package gtk+2.0 was not found in the pkg-config search path. 
Perhaps you should add the directory containing `gtk+2.0.pc' 
to the PKG_CONFIG_PATH environment variable 
No package 'gtk+2.0' found 
In file included from ./main.m:1:0: 
./MainWindow.h:1:20: fatal error: gtk/gtk.h: No such file or directory 
compilation terminated. 
In file included from ./MainWindow.m:1:0: 
./MainWindow.h:1:20: fatal error: gtk/gtk.h: No such file or directory 
compilation terminated. 

私はそれを解決し、この問題を解決するまでここに戻ってきます。

+0

'MainWindow.h'の上部近く'の#include "GTK/gtk.h" 'のようなものはおそらくあります。その行を表示して、そのファイルが実際にどこにあるかを作業ディレクトリに対して相対的に教えてください。 – Beta

+0

私は自分の仕事をgithubにコミットしました。あなたもチェックできます。ここ あなたは の#import の#importを含んでいる必要があり、<財団/ NSObject.h> の#import <財団/ NSString.hを> https://github.com/jmolinaso/GNUlog514 –

+0

しかし、あなたが持っていません '/gtk/gtk.h'を作業ディレクトリに入れていますか? – Beta

答えて

1

私はそれを動作させました。私はobjcヘッダーを使用します。

私のMakefileはその

# Suffixes 
%.o : %.m 
    $(CC) $(CCFLAGS) $(CCGTK) -c -o [email protected] $^ 

# Macros 
CC = gcc 
CCFLAGS = -lobjc 
CCGTK = `pkg-config --cflags --libs gtk+-2.0` 
SOURCES= $(wildcard *.m) 
OBJECTS= $(SOURCES:.m=.o) 
PROG = glog514 

# Targets 
all: $(SOURCES) $(PROG) 

$(PROG): $(OBJECTS) 
    $(CC) -o [email protected] $(OBJECTS) $(CCFLAGS) $(CCGTK) 

clean: 
    rm -f $(OBJECTS) $(PROG) 

のように見え、自分で自分のmain.mを試してみたい場合は

#import "MainWindow.h" 

int main(int argc, char *argv[]) { 

    //init gtk engine 
    gtk_init(&argc, &argv); 

    //set up GUI 
    MainWindow *mainWindow = [[MainWindow alloc] initWithArgCount:&argc ArgVals:argv]; 

    //begin the GTK loop 
    [mainWindow startGtkMainLoop]; 

    //free the GUI 
    [mainWindow free]; 

    //exit application 
    return 0; 
} 

私MainWindow.h

#include <objc/Object.h> 
#include <gtk/gtk.h> 

id myMainWindow; 

@interface MainWindow:Object 
{ 
    // Main GTKWindow 
    GtkWidget *mainWindow; 
    GtkWidget *button; 
} 

-(id)initWithArgCount:(int *)argc ArgVals:(char *[])argv; 

-(void)destroyWidget; 

-(void)startGtkMainLoop; 

-(void)printSomething; 

void on_MainWindow_destroy(GtkObject *object, gpointer user_data); 

void on_btnPushMe_clicked(GtkObject *object, gpointer user_data); 

@end 

私MainWindow.m

#include "MainWindow.h" 
#include <gtk/gtk.h> 

@implementation MainWindow 


-(id)initWithArgCount:(int *)argc ArgVals:(char *[])argv { 
    //call parent class’ init 
    if (self = [super init]) { 
    //setup the window 
    mainWindow = gtk_window_new (GTK_WINDOW_TOPLEVEL); 

    gtk_window_set_title (GTK_WINDOW (mainWindow), "Hello World"); 
    gtk_window_set_default_size(GTK_WINDOW(mainWindow), 230, 150); 

    //setup the button 
    button = gtk_button_new_with_label ("Push me!"); 

    gtk_container_add (GTK_CONTAINER (mainWindow), button); 

    //connect the signals 
    g_signal_connect (mainWindow, "destroy", G_CALLBACK (on_MainWindow_destroy), NULL); 
    g_signal_connect (button, "clicked", G_CALLBACK (on_btnPushMe_clicked), NULL); 

    //force show all 
    gtk_widget_show_all(mainWindow); 
    } 

    //assign C-compatible pointer 
    myMainWindow = self; 

    //return pointer to this object 
    return self; 
} 

-(void)startGtkMainLoop { 
    //start gtk loop 
    gtk_main(); 
} 

-(void)printSomething{ 

} 

-(void)destroyWidget{ 
    myMainWindow = NULL; 

    if(GTK_IS_WIDGET (button)){ 
    //clean up the button 
    gtk_widget_destroy(button); 
    } 

    if(GTK_IS_WIDGET (mainWindow)){ 
    //clean up the main window 
    gtk_widget_destroy(mainWindow); 
    } 
} 

-(void)dealloc{ 
    [self destroyWidget]; 

    [super dealloc]; 
} 

void on_MainWindow_destroy(GtkObject *object, gpointer user_data){ 
    //exit the main loop 
    gtk_main_quit(); 
} 

void on_btnPushMe_clicked(GtkObject *object, gpointer user_data){ 
    printf("Button was clicked\n"); 

    //call Objective-C function from C function using global object pointer 
    [myMainWindow printSomething]; 
} 

@end 

すべてのファイルを同じフォルダに入れてmakeを実行すると、glog514というコンパイル済みのファイルが作成され、それを実行するとnice gtkウィンドウが表示されます。

./glog514

歓声、

関連する問題