开发者

Objective-c compiling issues... help?

Please forgive me since this question i am also sure, has been asked before i expect. The problem i am having is that i am trying to learn obj-c on a pc seeing as i do not have a mac yet. i am using Jedit to write my code and GNUstep to compile and create. however my problem is occurring when trying to compile using the #include

according to the book i am using to learn as well as everything else i have found online (and do not quite understand), i need either a make file or point my compiler at a library or directory. i would prefer the make file i suppose as it seems a lot easier and faster to test things and learn.

the main question then is this: i seem to have found a GNUmake file that came with the online additions to my book, but i do not know how to use it or where to go with it. so if some one can please point out for me a easy to understand process or knows开发者_开发知识库 of such process and would be willing to impart that wisdom, it would be much appreciated.

Thank you kindly for your time


Assuming you have installed GNUstep correctly and your environment is set up (all explained in the documentation) then you actually need to create a blank GNUmakefile in the root directory of your project and list your sources in that.

Here's a simple "Hello World" command line example:

Create a directory to contain your source code. HelloWorld would be a good idea.

In that, create files main.m, Greeter.m and Greeter.h.

We'll create the Greeter class which will just have one method -sayHelloToRecipient:.

In the Greeter.h:

#import <Foundation/Foundation.h>

@interface Greeter : NSObject {
}

-(void)sayHelloToRecipient:(NSString *)recipientName;

@end

And in the Greeter.m:

#import "Greeter.h"

@implementation Greeter

-(void)sayHelloToRecipient:(NSString *)recipientName {
    NSLog(@"Hello %@!", recipientName);
}

@end

Your main.m file simply includes the Greeter and invokes it with the argument @"World".

#import "Greeter.h"

int main(void) {
    Greeter *greeter = [[Greeter alloc] init];
    [greeter sayHelloToRecipient:@"World"];
    [greeter release];
    return 0;
}

Now you've got your sources ready to build, you just need to create a GNUmakefile. Make an empty file, and start it with the line: include $(GNUSTEP_MAKEFILES)/common.make, ending with the line include $(GNUSTPEP_MAKEFILES)/tool.make.

The first line includes all the other makefiles and targets GNUstep provides. The last line includes the makefiles needed to produce a command line tool. If you were build a GUI app you'd include application.make. For frameworks you'd include framework.make etc.

include $(GNUSTEP_MAKEFILES)/common.make

# Your project-specific directives will go here

include $(GNUSTEP_MAKEFILES)/tool.make

The stuff you put between these lines is the stuff that changes from project to project.

The complete GNUmakefile looks like this:

include $(GNUSTEP_MAKEFILES)/common.make

HELLO_ROOT_DIR = $(abspath .)

GNUSTEP_BUILD_DIR = $(HELLO_ROOT_DIR)/build

TOOL_NAME = HelloWorld

HelloWorld_OBJC_FILES = \
    Greeter.m \
    main.m

include $(GNUSTEP_MAKEFILES)/tool.make

HELLO_ROOT_DIR is entirely optional and is just a variable that saves me from having to re-type the path to the root as the project grows (and so does the complexity of the makefile).

TOOL_NAME is required for a command line tool and specifies both the output filename, and determines what you need to use for the *_OBJC_FILES line (i.e. in this case I need to use HelloWorld_OBJC_FILES because TOOL_NAME is "HelloWorld".

With this in place, provided you're in the same directory as the makefile, you should be able to just type `make' to build the tool. It will create a "build" directory and inside that you'll find the executable. This one when invoked just outputs:

-$  ./build/obj/HelloWorld 
2010-11-28 03:36:28.459 HelloWorld[12949] Hello World!

None of this will work however if your environment is not configured correctly. I've never done this on Windows, but I assume the same principles apply. That's running a shell script on Linux/UNIX (running batch file on Windows?) in order to configure the environment.

-$  . /usr/share/GNUstep/Makefiles/GNUstep.sh

You can check if it's correctly configured by printing the environment variable GNUSTEP_MAKEFILES to the console:

-$  echo $GNUSTEP_MAKEFILES

(Not sure how you do this on Windows)

If it outputs nothing, your environment is not correctly configured and/or GNUstep is not correctly installed. If it outputs a path, you should be safe to run `make'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜