Construction of Objective-C Integrated Development Environment (IDE) under Windows

(zhuang) Building Objective-C Integrated Development Environment (IDE) under Windows
 
Pitaya software released on 2014-04--04
 

Construction of Objective-C Integrated Development Environment (IDE) under Windows (1)

Objective-C is the programming language of Apple software. If you want to learn and debug on the computer, it is much more convenient to have an integrated development environment (IDE). There are three types of methods to build an integrated development environment for Objective-C:

1. Using Apple's platform, the integrated development environment uses Xcode. But if you don’t have an Apple platform and want to learn Objective-C in a Windows environment, you can use the following two methods:

2. Set up an Apple virtual machine in the Windows environment, but this requires high performance on the personal computer, not all personal computers can do it, and the running speed of the virtual machine is also relatively slow;

3. Use the Codeblocks IDE development environment, configure it, and build an integrated development environment that supports the compilation, debugging, and operation of Object-C. This method has almost no requirements on the performance of the personal computer and can be built quickly. This article introduces this method.

1. Install the Object-C compiler

There are many compilers for Objective-C. This article introduces the use of GnuStep. The URL is http://www.gnustep.org/experience/Windows.html. You can download the Windows version of the gcc compiler from here:

Go to the download page, download the above three software packages, and install them, for example, to D:\GNUstep. About the functions of these three software packages, you can check them online, so I won’t repeat them here.

2. Install the integrated development environment of Objective-C

We choose to use CodeBlocks IDE as the integrated development environment for Objective-C, the download address is: http://www.codeblocks.org/.

3. Development environment configuration

Through the configuration of Code blocks, the construction of the Objective-C development environment is completed step by step. CodeBlocks, you can see this screen:

Step 1: Configure the compiler

Go to Settings->Compiler and debugger..., select the GNU GCC Compiler compiler, press the "Copy" button, and rename it to "GNUstep MinGW Compiler" and save. As shown in the picture:

Then enter the Other Options tab and enter:

-fconstant-string-class=NSConstantString -std=c99 As shown in the figure:

Step 2: Linker Settings Linker settings

Add two files in Link Libraries, as shown in the figure.

They are under D:\GNUstep\GNUstep\System\Library\Libraries:

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

Step 3: Specify the search directory Search directories (GNUstep needs to be pre-installed)

<!--[if !supportLists]-->1) <!--[endif]-->Compiler (compiler) is set to D:\GNUstep\GNUstep\System\Library\Headers;

2) Linker (connector) is set to D:\GNUstep\GNUstep\System\Library\Libraries;

Step 4: Add Objective-C file type support

<!--[if !supportLists]--> 1) <!--[endif]-->Enter Settings->Environment..., select Files extension handling to add *.m. As shown in the picture:

2) Enter Project->Project tree->Edit file types & categories... , add *.m to the file type list under Sources. As shown in the picture:

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


Guess you like

Origin blog.csdn.net/zp1990412/article/details/42754959