discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Hello World not getting off the ground


From: Richard Frith-Macdonald
Subject: Re: Hello World not getting off the ground
Date: Sat, 1 Jul 2017 20:51:30 +0100

> On 1 Jul 2017, at 18:45, Jay Versluis <versluis2000@yahoo.com> wrote:
> 
> Dear friends,
> 
> I’m a new GNUstep user with a decent amount of Objective-C experience through 
> Xcode on the Mac. I thought I could put my knowledge to good use for 
> developing cross platform applications, and thought GNUstep might be the 
> answer. However, after a week of trying every available tip I could find on 
> the web, I still can’t get a simple Hello World project to compile without 
> errors. I present to you my case.
> 
> My code is as simple as this:
> 
> #import <Foundation/Foundation.h>
> 
> int main(int argc, const char * argv[]) {
>     @autoreleasepool {
>         // insert code here...
>         NSLog(@"Hello, World!\n");
>         NSLog(@"Made with Objective-C\n");
>     }
>     return 0;
> }
> 
> I’m trying to compile this with gcc `gnustep-config —objc-flags` 
> `gnustep-config —base-libs` -o hello *.m
> 
> The compiler throws an error related to @autorelease, something that Xcode 
> likes adding when creating from a template. No trouble, I’ll take that 
> statement out, so we’re left with this:
> 
> #import <Foundation/Foundation.h>
> 
> int main(int argc, const char * argv[]) {
>    
>       NSLog(@"Hello, World!\n");
>       NSLog(@"Made with Objective-C\n");
>     
>     return 0;
> }
> 
> Compiling with the same statements yields no errors. I’d say “hurra”, however 
> when I run the app, I get what feels like several hundred lines or errors, 
> all beginning with “autorelease pool called for object”, followed by a 
> variety of NSObjects such as NSUserDefaults, NSDateMalloc, NSCalendarDate as 
> well as stuff like GSDictionary, GSInlineString, GSAbsoluteTime - in short, 
> nothing that I’ve called here. And it only happens at runtime. Another 
> variation on the code was to make the main method a (void) method, but that 
> didn’t make a difference.
> 
> Why is this happening? What am I doing wrong here?
> 
> What’s perhaps interesting is that I’m getting the exact same problem under 
> CentOS 7, Ubuntu 16, as well as Windows 10. Yes indeed, I’ve tried them all! 
> On Windows I’ve also tried compiling with a GNUmakefile. The compilation 
> works without a hitch, however I still get the above errors by the dozen. I 
> did try clang on CentOS too, but not knowing the right flags didn’t get me 
> anywhere. I wish everything was as easy as pressing that button in Xcode… but 
> alas it is not. 
> 
> For the makefile I’ve loosely followed this guide: 
> http://www.gnustep.org/resources/documentation/Developer/Base/ProgrammingManual/manual_1.html#Building-Your-First-Objective_002dC-Program
>  I’ve even tried to compile the source code in the example, but the standard 
> C library can’t be found on Windows. On that note, both Gorm and Project 
> Center refuse to start and quit with a Runtime Error. Perhaps it’s a sign. 
> Maybe I’m just not meant to use GNUstep.
> 
> But before giving up, I thought I’d ask you experienced folks for help. 
> aeruder was very kind yesterday on IRC already, with his help at least 
> something compiles on my systems.
> 
> Any tips to guides are welcome! I’d love to use GNUstep, even if GNUstep 
> doesn’t want to be used :-) 

Your copy of gnustep is not built to use the 'next generation' runtime, so the 
'@autoreleasepool' syntax is not understood.  You can write code using the 
original form:

int main(int argc, const char * argv[]) {
    NSAutoreleasePool   *pool = [NSAutoreleasePool new];
        // insert code here...
        NSLog(@"Hello, World!\n");
        NSLog(@"Made with Objective-C\n");
   [pool release];
    return 0;
}

or, perhaps better, you could use the GNUstep macros which aim at making it 
esier to write portable code (to work in both the classic and the next 
generation environments:

int main(int argc, const char * argv[]) {
    CREATE_AUTORELEASE_POOL(pool);
        // insert code here...
        NSLog(@"Hello, World!\n");
        NSLog(@"Made with Objective-C\n");
   RELEASE(pool);
    return 0;
}

In any case, the reason you are getting a lot of warnings uising
int main(int argc, const char * argv[]) {
        // insert code here...
        NSLog(@"Hello, World!\n");
        NSLog(@"Made with Objective-C\n");
    return 0;
}
is that the two NSLog() calls are being executed outside of an autorelease 
pool, so any temporary objects those functions create are leaked memory.  On a 
simple test program that leaked memorym is not an issue and the program will 
still run perfectly well, but on a larger program that is intended to keep 
running for a long time, memory leaks are a major issue (so you do need to 
ensure that autorelease pools are created and destroyed when the objects in the 
pools are no longer needed.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]