Search code examples
iosobjective-cprintfnslog

Objective-C Does not Print (NSLog and PrintF Not WORKING!)


I have searched for hours, tried more than 20 files, and everytime, running in Xcode or through terminal. None will present any output. I restarted terminal and restarted computer. Still no luck. At this point, I have absolutely no clue what's going on.

#import <Foundation/Foundation.h>

int main(int argc, char** argv)
{
  NSLog(@"Testing\n");
  printf("Hello\n");
}

In Terminal

gcc -framework Foundation hello.m -o

or

clang -framework Foundation hello.m -o

NEITHER WORK!

I could type the other 50 ones I tried, but let's start very very simple. After running it, there is no output to console. Confused as hell. Everywhere shows output, I tried to switch computers and run it, same thing. What the hell do you set or do to turn on log out put for Objective-C. No tutorials or anywhere covers this.

enter image description here

Nada printed.


Solution

  • You only compile the file and do not save the result. -o option is for write output and because you do not provide a filename, then the compiler just ignores the result and do not save it. To run it use the following commands:

    clang -framework Foundation hello.m -o test
    ./test
    

    or, as a shortcut,

    clang -framework Foundation hello.m -o test && ./test
    

    As you can see, now we specify the name for the output file as test and then execute it. Now you will be able to see the output.

    Also, be aware that NSLog no longer prints to the stdout and redirects all messages to the OSLog subsystem. You can check such logs in the Console.app. Check the Unified Logging and Activity Tracing WWDC session if you want to know more.