I have the following objective-c snippet in my hello world example:
//hello.m
#import <Foundation/Foundation.h>
#import "hello.h"
void sayHello()
{
#ifdef FRENCH
NSString *helloWorld = @"Bonjour Monde!\n";
#else
NSString *helloWorld = @"Hello World\n";
#endif
printf("%s", [helloWorld UTF8String]);
}
//main.m
#import <Foundation/Foundation.h>
#import "hello.h"
int main (int argc, const char * argv[])
{
sayHello();
return 0;
}
building this stuff on osx works fine and runs as expected. But when compiling/linking it on ubuntu (using GNUStep) results in an segmentation fault when executing the binary. I nailed it down to the casting operation in the printf statement, but I have no clue what I'm doing wrong here or how I can solve this.
Interesting note: This works fine when using gcc toolchain to build the executable. I just see this issue when building it with clang on ubuntu.
Any help is very much appreciated.
[helloWorld UTF8String]
is a method call, not a cast. Please provide the rest of your code for context. What you have posted won't compile (no functions/methods, etc) – Woodsonconst char*
version of the NSString. It's existence is tied to the lifetime of the NSString object in a manner that could be easily mucked up if the storage management environment is even slightly corrupt. – Charterhousemain()
function as its entry point. I'd think your code would fail to compile without that, rather than segfault, but I suppose it's possible that gcc compiles something which then falls over at runtime. Try putting this inside amain()
. – Wristwatch