简体   繁体   中英

Xcode: Objective-C build and run from command line

I have the following file:

doSomething.m

with the following implementation:

#import <Foundation/Foundation.h>

int main(int argc, char * argv[]) {
    NSString * appDelegateClassName;
    @autoreleasepool {
        NSLog(@"main");
    }
    return 0;
}

if I want to compile I need to run the following command:

clang -framework Foundation doSomething.m -o doSomething-exec

and to see any output and I need to execute the following command:

./doSomething-exec 

My question to you guys there is a way just to build/execute the code in the doSomething.m ?

I don't want to be compiling and generating the output file and then executing the executable.

My question to you guys there is a way just to build/execute the code in the doSomething.m?

Sure. Write a build script. It's very common for projects of any size to include a make file or some other sort of script that takes care of all the steps needed to build the project, often with a variety of parameters for building for different targets or with different options. Scripts are a great way to simplify repetitive operations, and it's exactly the right way to handle the repetitive task that you're facing. If you've got lots of these single-file projects, you could have the script take the name of the source file as a parameter.

Your script doesn't have to be anything fancy: just list the commands that you'd normally issue in a text file prefixed with #!/bin/sh (or whatever your favorite shell is), like:

#!/bin/sh
clang -framework Foundation doSomething.m -o doSomething-exec
./doSomething-exec

Then make your script executable, and you're ready to go.

I don't want to be compiling and generating the output file and then executing the executable.

Another option is to create an Xcode project with a target for your doSomething product. That's a pretty typical modus operandi for developers working in the usual iterative edit-compile-run fashion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM