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


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。图形界面的设计全部在代码中。

 

 

  1. 第一步:使用codeblocks新建一个c的console程序。这里命名该工程名为gui,如图所示:
  2. 添加一个main.c文件,将下面的代码拷贝并覆盖main.c中内容#include "AppController.h"
    1. #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);
      }

     

  3. 添加一个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_ */		
  4. 实现一个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
  5. 接下来是配置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 = "";
    }
  6. 最后是比较重要的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
  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. -------------- 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
     
  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中编译运行。效果如图

     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值