When you create a Command Line Tool project in Xcode you get this, in main.m:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
}
return 0;
}
I can run this from Xcode. But I want to compile it so that I can run it from Terminal. What are the steps?
Assuming your executable is named "my_program", and it's in the "/foo/bar/Debug" directory:
cd /foo/bar
./my_program
If you aren't sure how to find the program file itself, you can right-click it (i.e.: the "product") and "Show in Finder" as shown in this screenshot:
You can have the terminal launch everytime you run the application through editing the scheme. I believe this became available from Xcode 8.0.
Access Edit Scheme
Switch Console
from Use Xcode
to Use Terminal
In Xcode 9 you can try the following (works for me in June 2018):
${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}
Normally Xcode will open terminal for you. If not, you may also turn off any debug related fields in the 'Info' tab. Hope this helps!
Full tutorial here: https://www.raywenderlich.com/163134/command-line-programs-macos-tutorial-2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With