
Windows下的Objective-C集成开发环境(IDE)的搭建 (二)
继上一步Windows下的Objective-C集成开发环境(IDE)的搭建 (一)配置运行命令行程序后,今天来讲解一下如何使用
codeblocks配置开发使用cocoa framework开发GUI程序。
#include "AppController.h"
#include <AppKit/AppKit.h>
int main(int argc, const char *argv[])
{
NSAutoreleasePool *pool;
AppController *delegate;
pool = [[NSAutoreleasePool alloc] init];
delegate = [[AppController alloc] init];
[NSApplication sharedApplication];
[NSApp setDelegate: delegate];
RELEASE(pool);
return NSApplicationMain (argc, argv);
}
我们使用一个helloworld开始旅程。
这个helloworld程序共有五个文件:main.m、AppController.h、AppController.m、helloworldInfo.plist和GNUmakefile。图形界面的设计全部在代码中。
-
第一步:使用codeblocks新建一个c的console程序。这里命名该工程名为gui,如图所示:
- 添加一个main.c文件,将下面的代码拷贝并覆盖main.c中内容#include "AppController.h"
-
#include <AppKit/AppKit.h>
int main(int argc, const char *argv[])
{
NSAutoreleasePool *pool;
AppController *delegate;
pool = [[NSAutoreleasePool alloc] init];
delegate = [[AppController alloc] init];
[NSApplication sharedApplication];
[NSApp setDelegate: delegate];
RELEASE(pool);
return NSApplicationMain (argc, argv);
}
- 添加一个AppController.h 头文件,内容如下:
#ifndef _AppController_H_
#define _AppController_H_
#include <Foundation/NSObject.h>
@class NSWindow;
@class NSTextField;
@class NSNotification;
@interface AppController : NSObject
{
NSWindow *window;
NSTextField *label;
}
- (void)applicationWillFinishLaunching:(NSNotification *) not;
- (void)applicationDidFinishLaunching:(NSNotification *) not;
@end
#endif /* _AppController_H_ */
- 实现一个AppController.h的AppController.m文件#include "AppController.h"
#include <AppKit/AppKit.h>
@implementation AppController
- (void) applicationWillFinishLaunching: (NSNotification *) not
{
/* Create Menu */
NSMenu *menu;
NSMenu *info;
menu = [NSMenu new];
[menu addItemWithTitle: @"Info"
action: NULL
keyEquivalent: @""];
[menu addItemWithTitle: @"Hide"
action: @selector(hide:)
keyEquivalent: @"h"];
[menu addItemWithTitle: @"Quit"
action: @selector(terminate:)
keyEquivalent: @"q"];
info = [NSMenu new];
[info addItemWithTitle: @"Info Panel..."
action: @selector(orderFrontStandardInfoPanel:)
keyEquivalent: @""];
[info addItemWithTitle: @"Preferences"
action: NULL
keyEquivalent: @""];
[info addItemWithTitle: @"Help"
action: @selector (orderFrontHelpPanel:)
keyEquivalent: @"?"];
[menu setSubmenu: info
forItem: [menu itemWithTitle:@"Info"]];
RELEASE(info);
[NSApp setMainMenu:menu];
RELEASE(menu);
/* Create Window */
window = [[NSWindow alloc] initWithContentRect: NSMakeRect(300, 300, 200, 100)
styleMask: (NSTitledWindowMask |
NSMiniaturizableWindowMask |
NSResizableWindowMask)
backing: NSBackingStoreBuffered
defer: YES];
[window setTitle: @"Hello World"];
/* Create Label */
label = [[NSTextField alloc] initWithFrame: NSMakeRect(30, 30, 80, 30)];
[label setSelectable: NO];
[label setBezeled: NO];
[label setDrawsBackground: NO];
[label setStringValue: @"Hello World"];
[[window contentView] addSubview: label];
RELEASE(label);
}
- (void) applicationDidFinishLaunching: (NSNotification *) not
{
[window makeKeyAndOrderFront: self];
}
- (void) dealloc
{
RELEASE(window);
[super dealloc];
}
@end
-
接下来是配置gui.plist文件:
{
ApplicationDescription = "Hello World Tutorial";
ApplicationIcon = "";
ApplicationName = HelloWorld;
ApplicationRelease = 0.1;
Authors = "";
Copyright = "Copyright (C) 200x by ...";
CopyrightDescription = "Released under...";
FullVersionID = 0.1;
URL = "";
}
-
最后是比较重要的GNUmakefile:
GNUSTEP_MAKEFILES=D:/GNUstep/GNUstep/System/Library/Makefiles
include $(GNUSTEP_MAKEFILES)/common.make
APP_NAME = GUI
$(APP_NAME)_HEADERS = AppController.h
$(APP_NAME)_OBJC_FILES = main.m AppController.m
$(APP_NAME)_RESOURCE_FILES = guiInfo.plist
include $(GNUSTEP_MAKEFILES)/application.make
- 上述步骤中比较关键的是GNUmakefile的编写,其中GNUSTEP_MAKEFILES是我指定的GNUStep的安装目录的makefiles目录,APP_NAME是配置app名称,headers是header文件,如果存在多个.h文件,可以指定为*.h,OBJC_FILES为指定.h实现文件,如果有多个可以指定为*.m, RESOURCE_FILES为整个工程的属性配置文件。整个工程的截图如下:

-
第二步:配置codeblocks,进入Settings->Compiler and Debugger->Gobal compiler setting选项卡中点击“copy”按钮,并将该compiler命令为GNU Compiler Cocoa(可以随意指定),如图:

- 选择Toolchain executables ,设置compiler's installation directory 为你安装GUNStep的目录,我但前目录为D:\GNUstep,然后配置c compiler ,c++ compiler 等可执行文件,注意这个文件在D:\GNUstep\bin目录下,如图:

- 注意下图所示的红色区域,一定要选择正确的make可执行文件,在GNUstep\msys\1.0\bin目录下,笔者的目录为D:\GNUstep\msys\1.0\bin\make.exe,配置正确之后,点击OK
- 右键点击工程“gui”,选择“properties”,在"project setting"中勾选”this is a custom Makefile"复选框,Makefile的名称必须为GNUmakefile, "Execution Directory"选择工程所在文件目录。如图所示

-
右键点击工程“gui”,选择“build options”,在debug分支中选择上一步配置好的compiler,即“ GNU Compiler Cocoa",然后在下方的"Make" commands中配置下面的选项,如下所示:

- 上述步骤完成之后,Antr+F11重新编译工程,如果没有问题的话,会有如下输出:
-
-------------- Clean: Debug in gui ---------------
Cleaned "gui - Debug"
-------------- Build: Debug in gui ---------------
Using makefile: GNUmakefile
This is gnustep-make 2.6.1. Type 'make.exe print-gnustep-make-help' for help.
Making all for app GUI...
Copying resources into the app wrapper...
Process terminated with status 0 (0 minutes, 1 seconds)
0 errors, 0 warnings
- 编译过程中可能出现以下错误:Compile-Error Windows : native-objc-exceptions does not match that of gnustep-base,解决办法如下,将 D:\GNUstep\GNUstep\System\Library\Headers\GNUstepBase\GSConfig.h :中BASE_NATIVE_OBJC_EXCEPTIONS 定义的值改为1即可。
- 至此,我们已经可以在codeblocks中编译基于cocoa框架的应用,但是编译出来的文件在目录APP_NAME.app文件夹中,如何才能让编译的文件在bin\Debug目录中呢?打开GNUmakefile可以得知,该文件导入了两个系统提供的make文件,一个是common.make,另外一个是application.make文件,找到application.make文件,该文件在笔者的文件路径为D:\GNUstep\GNUstep\System\Library\Makefiles\Instance\application.make,打开该文件,修改APP_DIR_NAME的值为 $(GNUSTEP_BUILD_DIR)/bin/Debug,APP_DIR的值为$(GNUSTEP_BUILD_DIR)/bin/Debug 即可,这样就可以实现
-
在codeblocks中编译运行。效果如图


- 大小: 29 KB

- 大小: 32.3 KB

- 大小: 46.4 KB

- 大小: 86.8 KB

- 大小: 4.1 KB

- 大小: 27.7 KB

- 大小: 20.5 KB

- 大小: 2.6 KB
分享到:
相关推荐
### Windows下的Objective-C集成开发环境的搭建 #### 一、前言 Objective-C是一种主要应用于苹果操作系统的编程语言,尤其适用于iOS和OS X应用程序的开发。对于初学者来说,搭建一个良好的集成开发环境...
在Windows操作系统上搭建Objective-C的集成开发环境(IDE)可能会比在macOS系统上稍微复杂一些,但通过一些工具和步骤,我们仍然可以构建一个功能完备的开发环境。本篇文章将详细介绍如何使用gnumstep和Code::Blocks这...
Objective-C是苹果软件的编程语言,想要上机学习、调试,有一个集成开发环境(IDE)方便很多。有三类方法搭建Objective-C的集成开发环境:<!--[if!supportLists]-->1)<!--[endif]-->使用苹果的平台,...
本文旨在详细介绍如何在 Windows XP 操作系统中搭建一套基于 GNUstep 和 CodeBlocks 的 Objective-C 集成开发环境(IDE)。该环境对于希望在 Windows 平台上进行 Objective-C 应用程序开发的开发者来说非常实用。 #...
Objective-C是Apple软件的编程语言,在学习、调试之前,需要搭建一个集成开发环境(IDE)。一般针对Objective-C的IDE开发环境的搭建方式有三类方法: 1. 拥有Apple平台(即苹果电脑一台),集成开发环境使用Xcode; ...
在Windows环境下进行...通过以上步骤,你应该能够在Windows环境下搭建起一个基本的Objective-C开发环境,并开始你的编程之旅。记住,实践是最好的老师,不断编写代码并解决遇到的问题,你会逐渐熟练掌握Objective-C。
1. **CodeBlocks IDE**:CodeBlocks是一款免费且开源的集成开发环境(IDE),支持多种编程语言,包括C++、C等。它具有强大的功能集和用户友好的界面。 - **推荐版本**:codeblocks-17.12mingw-setup.exe,因为该版本...
Objective-C是Apple软件的编程语言,在学习、调试之前,需要搭建一个集成开发环境(IDE)。一般针对Objective-C的IDE开发环境的搭建方式有三类方法: 1. 拥有Apple平台(即苹果电脑一台),集成开发环境使用Xcode; ...
Objective-C是Apple软件的编程语言,在学习、调试之前,需要搭建一个集成开发环境(IDE)。一般针对Objective-C的IDE开发环境的搭建方式有三类方法: 1. 拥有Apple平台(即苹果电脑一台),集成开发环境使用Xcode; ...
- **ProjectCenter**:一个集成开发环境(IDE),功能类似于Xcode。 - **gnustep-examples**:包含了各种GUI示例程序。 这些包可以从官方网站下载源码包,解压后依次执行`./configure`、`make`和`make install`命令...
然后,启动Xcode集成开发环境(IDE)。 2. **配置Xcode**:首次启动Xcode时,系统会提示进行一些初始设置。建议保持默认设置不变,除非有特殊需求。可以通过“偏好设置”面板随时调整设置。 3. **创建新项目**:...
为了能够在计算机上进行 Objective-C 的学习与实践,拥有一个集成开发环境(Integrated Development Environment, IDE)是非常必要的。本文主要介绍如何在非苹果平台(例如 Windows)上,通过 Codeblocks IDE 搭建一...
### Objective-C 在 Win7 环境下的集成开发环境(IDE)搭建方法 #### 一、概述 Objective-C 是一种面向对象的编程语言,最初由苹果公司用于开发 macOS 和 iOS 操作系统上的应用程序。尽管 Objective-C 的主要开发...
- **Xcode**:Xcode是苹果提供的官方集成开发环境(IDE),支持Objective-C和Swift等多种语言。 - **模拟器**:Xcode内置了iOS和macOS的模拟器,可以在开发过程中测试应用。 - **Cocoa Touch框架**:这是用于开发iOS...
为了在Windows下编译Objective-C代码,我们需要GnuStep,这是一个开源项目,提供了Objective-C的编译器。访问`http://www.gnustep.org/experience/Windows.html`下载Windows版本的GCC编译器。通常需要下载三个...