Windows下的Objective-C集成开发环境(IDE)的搭建

Windows下的Objective-C集成开发环境(IDE)的搭建 (一)

Objective-C是苹果软件的编程语言,想要上机学习、调试,有一个集成开发环境(IDE)方便很多。有三类方法搭建Objective-C的集成开发环境:

<!--[if !supportLists]-->1) <!--[endif]-->使用苹果的平台,集成开发环境使用Xcode。但如果没有苹果平台,想在Windows环境下学习Objective-C,可以采用以下两种方法:

<!--[if !supportLists]-->2) <!--[endif]-->在Windows环境下设置一个苹果虚拟机,但这对个人电脑的性能要求较高,不是所有个人电脑都可以,而且虚拟机的运行速度也较慢;

<!--[if !supportLists]-->3) <!--[endif]-->采用Codeblocks IDE开发环境,对其进行配置,搭建成支持Object-C的编译、调试、运行的集成开发环境。这种方法对个人电脑的性能几乎没有要求,可以快速构建,本文介绍的是这一种方法。

1、安装Object-C的编译器

Objective-C的编译器有很多,本文介绍使用GnuStep,网址是http://www.gnustep.org/experience/Windows.html,从这里可以下载Windows版本的gcc编译器:

进入下载页面,下载上面3个软件包,进行安装,例如安装到D:\GNUstep。关于这3个软件包的作用,可以在网上查询,不再赘述。

2、安装Object-C的集成开发环境

我们选择用CodeBlocks IDE作为Objective-C的集成开发环境,下载地址是:http://www.codeblocks.org/。

3、开发环境配置

通过对Code blocks的配置,一步步完成Objective-C开发环境的搭建。CodeBlocks,可以看见这样的画面:

第一步:配置编译器

进入Settings->Compiler and debugger...,选择GNU GCC Compiler编译器,按“Copy”按钮,并重新命名为“GNUstep MinGW Compiler“并保存。如图:

之后进入Other Options 分页,录入:

-fconstant-string-class=NSConstantString -std=c99 如图:

第二步:连接器设置 Linker stettings

在连接库(Link Libraries)中添加两个文件,如图。

它们在D:\GNUstep\GNUstep\System\Library\Libraries下面:

libgnustep-base.dll.a
libobjc.dll.a

第三步:指定搜索目录Search directories(需要预先安装好GNUstep)

<!--[if !supportLists]-->1) <!--[endif]-->Compiler(编译器)设置为D:\GNUstep\GNUstep\System\Library\Headers;

2) Linker(连接器)设置为D:\GNUstep\GNUstep\System\Library\Libraries;

第四步:添加Objective-C文件类型支持

<!--[if !supportLists]--> 1) <!--[endif]-->进入Settings->Environment...,选择 Files extension handling 添加*.m。如图:

2) 进入 Project->Project tree->Edit file types & categories... ,在Sources, 下面添加 *.m到文件类型列表中。如图:

3) 进入 Settings->Editor...,选择 Syntax highlighting,点击“Filemasks....”按钮,在弹出框尾部添加*.m 到文件类型。如图:

4) 点击“Keywords...”按钮 (紧靠Filemasks...按钮) 添加下面Object-C的关键字到Edit Keywords列表中。如图。
@interface @implementation @end @class @selector @protocol @public @protected @private id BOOL YES NO SEL nil NULL self @protocol

<!--[if !supportLists]-->4. <!--[endif]-->代码测试

上述开发环境配置完成后,就可以开始代码测试了。

首先,新建一个工程,选择File->New->Project…,会出现一个工程类型窗口,选择Console Application,然后按照工程建立指引,建立一个mytest的工程,并将main.c的文件更名为main.m,录入以下代码:

#import <Foundation/Foundation.h>
int main (int argc, const char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSLog(@"%@",@"hello world");
    [pool drain];
    return 0;
}

如图:

之后再开始编译运行:Buid –> Run… 如果出现以下窗口,恭喜你,你已经成功的搭建了Windows下的Objective-C的集成开发环境。

如果顺利编译通过并运行,那么恭喜object-c的windows开发环境已经配置好了,如果你想了解并运行cocoa框架的话
还需要对codeblocks进行下一步配置。

Windows下的Objective-C集成开发环境(IDE)的搭建 (二)

继上一步Windows下的Objective-C集成开发环境(IDE)的搭建 (一)配置运行命令行程序后,今天来讲解一下如何使用
codeblocks配置开发使用cocoa framework开发GUI程序。

Java代码

#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 头文件,内容如下:

<span style="white-space: normal; background-color: #ffffff;">#ifndef _AppController_H_</span> 

#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_ */<span style="white-space: pre;">		</span>

实现一个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重新编译工程,如果没有问题的话,会有如下输出:

<span style="white-space: normal; background-color: #ffffff;">
 Clean: Debug in gui 
</span>

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





  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值