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

本文介绍如何在CodeBlocks中配置Cocoa框架进行GUI程序开发。通过创建一个简单的Hello World程序,详细展示了从新建项目到配置编译环境的全过程。


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

 

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

codeblocks配置开发使用cocoa framework开发GUI程序。

 

Java代码
 收藏代码
  1. #include "AppController.h"  
  2. #include <AppKit/AppKit.h>  
  3.   
  4. int main(int argc, const char *argv[])  
  5. {  
  6.    NSAutoreleasePool *pool;  
  7.    AppController *delegate;  
  8.   
  9.    pool = [[NSAutoreleasePool alloc] init];  
  10.    delegate = [[AppController alloc] init];  
  11.   
  12.    [NSApplication sharedApplication];  
  13.    [NSApp setDelegate: delegate];  
  14.   
  15.    RELEASE(pool);  
  16.    return NSApplicationMain (argc, argv);  
  17. }  
 

 

我们使用一个helloworld开始旅程。

 

 

    这个helloworld程序共有五个文件:main.m、AppController.h、AppController.m、helloworldInfo.plist和GNUmakefile。图形界面的设计全部在代码中。

 

 

  1. 第一步:使用codeblocks新建一个c的console程序。这里命名该工程名为gui,如图所示:
  2. 添加一个main.c文件,将下面的代码拷贝并覆盖main.c中内容#include "AppController.h"
    1. Cpp代码
       收藏代码
      1. #include <AppKit/AppKit.h>  
      2.   
      3. int main(int argc, const char *argv[])  
      4. {  
      5.    NSAutoreleasePool *pool;  
      6.    AppController *delegate;  
      7.   
      8.    pool = [[NSAutoreleasePool alloc] init];  
      9.    delegate = [[AppController alloc] init];  
      10.   
      11.    [NSApplication sharedApplication];  
      12.    [NSApp setDelegate: delegate];  
      13.   
      14.    RELEASE(pool);  
      15.    return NSApplicationMain (argc, argv);  
      16. }  

     

  3. 添加一个AppController.h 头文件,内容如下:
    Cpp代码
     收藏代码
    1. <span style="white-space: normal; background-color: #ffffff;">#ifndef _AppController_H_</span>  
    Cpp代码
     收藏代码
    1. #define _AppController_H_  
    2.   
    3. #include <Foundation/NSObject.h>  
    4.   
    5. @class NSWindow;  
    6. @class NSTextField;  
    7. @class NSNotification;  
    8.   
    9. @interface AppController : NSObject  
    10. {  
    11.    NSWindow *window;  
    12.    NSTextField *label;  
    13. }  
    14.   
    15. - (void)applicationWillFinishLaunching:(NSNotification *) not;  
    16. - (void)applicationDidFinishLaunching:(NSNotification *) not;  
    17.   
    18. @end  
    19.   
    20. #endif /* _AppController_H_ */<span style="white-space: pre;">      </span>  
  4. 实现一个AppController.h的AppController.m文件#include "AppController.h"
    Cpp代码
     收藏代码
    1. #include <AppKit/AppKit.h>  
    2.   
    3. @implementation AppController  
    4. - (void) applicationWillFinishLaunching: (NSNotification *) not  
    5. {  
    6.    /* Create Menu */  
    7.    NSMenu *menu;  
    8.    NSMenu *info;  
    9.   
    10.    menu = [NSMenu new];  
    11.    [menu addItemWithTitle: @"Info"  
    12.                    action: NULL  
    13.             keyEquivalent: @""];  
    14.    [menu addItemWithTitle: @"Hide"  
    15.                    action: @selector(hide:)  
    16.             keyEquivalent: @"h"];  
    17.    [menu addItemWithTitle: @"Quit"  
    18.                    action: @selector(terminate:)  
    19.             keyEquivalent: @"q"];  
    20.   
    21.    info = [NSMenu new];  
    22.    [info addItemWithTitle: @"Info Panel..."  
    23.                    action: @selector(orderFrontStandardInfoPanel:)  
    24.             keyEquivalent: @""];  
    25.    [info addItemWithTitle: @"Preferences"  
    26.                    action: NULL   
    27.             keyEquivalent: @""];  
    28.    [info addItemWithTitle: @"Help"  
    29.                    action: @selector (orderFrontHelpPanel:)  
    30.             keyEquivalent: @"?"];  
    31.   
    32.    [menu setSubmenu: info   
    33.             forItem: [menu itemWithTitle:@"Info"]];  
    34.    RELEASE(info);  
    35.   
    36.    [NSApp setMainMenu:menu];  
    37.    RELEASE(menu);  
    38.   
    39.    /* Create Window */  
    40.    window = [[NSWindow alloc] initWithContentRect: NSMakeRect(300, 300, 200, 100)  
    41.                               styleMask: (NSTitledWindowMask |  
    42.                                           NSMiniaturizableWindowMask |  
    43.                                           NSResizableWindowMask)  
    44.                                backing: NSBackingStoreBuffered  
    45.                                defer: YES];  
    46.    [window setTitle: @"Hello World"];  
    47.   
    48.    /* Create Label */  
    49.    label = [[NSTextField alloc] initWithFrame: NSMakeRect(30, 30, 80, 30)];  
    50.    [label setSelectable: NO];  
    51.    [label setBezeled: NO];  
    52.    [label setDrawsBackground: NO];  
    53.    [label setStringValue: @"Hello World"];  
    54.   
    55.    [[window contentView] addSubview: label];  
    56.    RELEASE(label);  
    57. }  
    58.   
    59. - (void) applicationDidFinishLaunching: (NSNotification *) not  
    60. {  
    61.    [window makeKeyAndOrderFront: self];  
    62. }  
    63.   
    64. - (void) dealloc  
    65. {  
    66.   RELEASE(window);  
    67.   [super dealloc];  
    68. }  
    69.   
    70. @end  
  5. 接下来是配置gui.plist文件:
    Cpp代码
     收藏代码
    1.  {  
    Cpp代码
     收藏代码
    1. ApplicationDescription = "Hello World Tutorial";  
    2. ApplicationIcon = "";  
    3. ApplicationName = HelloWorld;  
    4. ApplicationRelease = 0.1;  
    5. Authors = "";  
    6. Copyright = "Copyright (C) 200x by ...";  
    7. CopyrightDescription = "Released under...";  
    8. FullVersionID = 0.1;  
    9. URL = "";  
  6. 最后是比较重要的GNUmakefile:
    Cpp代码
     收藏代码
    1. GNUSTEP_MAKEFILES=D:/GNUstep/GNUstep/System/Library/Makefiles  
    2.   
    3. include $(GNUSTEP_MAKEFILES)/common.make  
    4.   
    5. APP_NAME = GUI  
    6. $(APP_NAME)_HEADERS = AppController.h  
    7. $(APP_NAME)_OBJC_FILES = main.m AppController.m  
    8. $(APP_NAME)_RESOURCE_FILES = guiInfo.plist  
    9.   
    10. include $(GNUSTEP_MAKEFILES)/application.make  
  7. 上述步骤中比较关键的是GNUmakefile的编写,其中GNUSTEP_MAKEFILES是我指定的GNUStep的安装目录的makefiles目录,APP_NAME是配置app名称,headers是header文件,如果存在多个.h文件,可以指定为*.h,OBJC_FILES为指定.h实现文件,如果有多个可以指定为*.m, RESOURCE_FILES为整个工程的属性配置文件。整个工程的截图如下:

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

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

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

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

  13. 上述步骤完成之后,Antr+F11重新编译工程,如果没有问题的话,会有如下输出:
  14. Cpp代码
     收藏代码
    1. <span style="white-space: normal; background-color: #ffffff;">-------------- Clean: Debug in gui ---------------</span>  
    Cpp代码
     收藏代码
    1. Cleaned "gui - Debug"  
    2.   
    3. -------------- Build: Debug in gui ---------------  
    4.   
    5. Using makefile: GNUmakefile  
    6. This is gnustep-make 2.6.1. Type 'make.exe print-gnustep-make-help' for help.  
    7. Making all for app GUI...  
    8.  Copying resources into the app wrapper...  
    9. Process terminated with status 0 (0 minutes, 1 seconds)  
    10. 0 errors, 0 warnings  
     
  15. 编译过程中可能出现以下错误: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即可。
  16. 至此,我们已经可以在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 即可,这样就可以实现
  17. 在codeblocks中编译运行。效果如图

     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值