为 Objective-C 和 gtk 创建 makefile

发布于 2024-12-11 05:35:48 字数 1603 浏览 1 评论 0原文

我是 Objective-C 和 makefile 的新手,目前我正在尝试通过 make 编译 Objective-C 和 Gtk+“hello world”。 make 代码如下

# 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 

,make 后得到以下输出。

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

您可能还需要任何其他信息,请尽管询问。

更新:

我还有其他可能有帮助的东西, 发出命令时

$ 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.

,我将修复该问题,然后回到此处不断更新此问题,直到解决。

I'm new in objective-c and makefiles, currently I'm trying to get an objective-c and Gtk+ "hello world" to compile via make.
The make code is as follows

# Suffixes

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

# 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 

and I get the following output after make.

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

Anything else you may need just ask.

UPDATE:

I've got something else that may help,
when issuing the command

$ 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

(Got the error gtk+2.0 to gtk+-2.0)I get the following output

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.

I'll get that fixed and come back here to keeping updated this question until solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠栀 2024-12-18 05:35:48

我让它工作了。长话短说,我不使用 Foundations 标头,而是使用 objc 标头。

我的 Makefile 看起来像这样

# Suffixes
%.o : %.m
    $(CC) $(CCFLAGS) $(CCGTK) -c -o $@ $^

# 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 $@ $(OBJECTS) $(CCFLAGS) $(CCGTK) 

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

如果你想自己尝试 my 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;
}

my 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

my 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 的编译文件,然后执行它你会得到漂亮的 gtk 窗口。

./glog514

干杯,

I got it to work. Long story short instead of using Foundations headers I use objc headers.

My Makefile looks like that

# Suffixes
%.o : %.m
    $(CC) $(CCFLAGS) $(CCGTK) -c -o $@ $^

# 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 $@ $(OBJECTS) $(CCFLAGS) $(CCGTK) 

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

And if you want to try by yourself my 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;
}

my 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

my 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

Just put all the files in the same folder and run make, you'll get a compiled file called glog514, then execute it and you'll get the nice gtk window.

./glog514

cheers,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文