Segmentation error while trying to print NSString as UTF8String

I have the following objective-c snippet in my welcome 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; } 

Creating this stuff in osx works fine and works as expected. But when compiling / linking it to ubuntu (using GNUStep), a segmentation error occurs when executing the binary. I nailed it to the casting operation in the printf statement, but I don’t know what I am doing wrong here or how I can solve it.

Interesting note: this works great when using the gcc toolchain to create an executable. I just see this problem creating it with clang on ubuntu.

Any help is greatly appreciated.

+2
objective-c gnustep
Jan 20 '14 at 19:30
source share
1 answer

To fix this problem, I ended up changing my code to the following:

 ... void sayHello() { #ifdef FRENCH NSString *helloWorld = @"${HELLO_WORLD_FRENCH}\\n"; #else NSString *helloWorld = @"${HELLO_WORLD}\\n"; #endif NSFileHandle *stdout = [NSFileHandle fileHandleWithStandardOutput]; NSData *strData = [helloWorld dataUsingEncoding: NSASCIIStringEncoding]; [stdout writeData: strData]; } ... 
+1
Jan 23 '14 at 9:24
source share
β€” -



All Articles