1

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;
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
squared
  • 45
  • 6
  • Your command line isn't compiling and linking the code for your song class along with the playList file. The error is a linker error. – rmaddy Sep 20 '15 at 04:49
  • @rmaddy Do you have any suggestions for what I can do to resolve the error? Should I change my console command? Thanks for your help. – squared Sep 20 '15 at 05:16
  • I'm not too familiar with using GNUStep or compiling Objective-C from the command line but it could be as simple as adding "song.m" just after "playList.m" on your `gcc` command line. – rmaddy Sep 20 '15 at 05:20
  • I tried adding song.m after playList.m and it compiled without errors! Unfortunately, the program now crashes when I try to add newSong to the array in the last line. – squared Sep 20 '15 at 05:28
  • You never initialize `savedSongsArray`. – rmaddy Sep 20 '15 at 05:29
  • Of course! Thank you so much. – squared Sep 20 '15 at 05:33

0 Answers0