I am new to Objective C and using GNUstep on Windows. When I try to compile the following code:
#import <Foundation/Foundation.h>
#import "song.h"
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Set up variables
//Read initial songs
NSError *error;
NSURL *playlistURL = [NSURL fileURLWithPath:@"C:\\ObjCPrograms\\PlayList\\playlist.txt"];
NSString *savedSongs = @"";
savedSongs = [[NSString alloc] initWithContentsOfURL:playlistURL encoding:NSUTF8StringEncoding error:&error];
//Separate initial songs into array
NSMutableArray *savedSongsArray;
NSMutableArray *songLine = (NSMutableArray *)[savedSongs componentsSeparatedByString:@"\n"];
int line = 0;
for(line; line < [songLine count]; line++)
{
NSMutableArray *lineContents = (NSMutableArray *)[[songLine objectAtIndex:line] componentsSeparatedByString:@"|"];
song *newSong = [[song alloc]init];
[song autorelease];
NSString *title = [lineContents objectAtIndex:0];
NSString *performer = [lineContents objectAtIndex:1];
NSString *genre = [lineContents objectAtIndex:2];
NSString *length = [lineContents objectAtIndex:3];
NSLog(@"%@|%@|%@|%@", title, performer, genre, length);
[newSong setTitle: title];
NSLog(@"here");
[newSong setPerformer: performer];
[newSong setGenre: genre];
[newSong setLength: length];
[savedSongsArray addObject:newSong];
}
using the command
gcc -o PlayList playList.m -I/c/GNUstep/GNUstep/System/Library/Headers \
-L /c/GNUstep/GNUstep/System/Library/Libraries -lobjc -lgnustep-base \
-fconstant-string-class=NSConstantString
I get the error message
playList.m:(.data+0x230): undefined reference to `__objc_class_name_song'
collect2: ld returned 1 exit status
I believe my issue is the declaration of newsong, because when I replace it with
song *newSong;
my code compiles. However, when I run this code, the program crashes when I am calling the setters for newSong:
[newSong setTitle: title];
I checked, and the setter is never even called. From what I have read, there may be an issue with linking, but I could not find any typos or missing code.
This is my setter method in the interface and in the implementation of song:
- (void) setTitle: (NSString *) newTitle;
- (void) setTitle: (NSString *) newTitle;
{
[newTitle retain];
[title release];
title = newTitle;
}