Xcode 命令行工具 - 如何在终端中运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19554822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:59:04  来源:igfitidea点击:

Xcode command line tool - how to run in terminal?

xcodecommand-line

提问by soleil

When you create a Command Line Tool project in Xcode you get this, in main.m:

当你在 Xcode 中创建一个命令行工具项目时,你会在 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?

我可以从 Xcode 运行它。但我想编译它,以便我可以从终端运行它。步骤是什么?

回答by Bob Murphy

Assuming your executable is named "my_program", and it's in the "/foo/bar/Debug" directory:

假设您的可执行文件名为“my_program”,并且它位于“/foo/bar/Debug”目录中:

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:

如果您不确定如何找到程序文件本身,您可以右键单击它(即:“产品”)和“在 Finder 中显示”,如下面的屏幕截图所示:

enter image description here

在此处输入图片说明

回答by joliejuly

In Xcode 9 you can try the following (works for me in June 2018):

在 Xcode 9 中,您可以尝试以下操作(2018 年 6 月适用于我):

  1. instead of 'edit scheme' click 'new scheme', give it a name and save
  2. now choose that new scheme you've just created and click 'edit scheme'
  3. go to the 'Info' tab and in a menu 'Executable' choose 'Other...'
  4. in file window go to search input field and type 'terminal' and click on its icon when you find it. Now you should see 'Terminal.app' in 'Executable' field
  5. go to the 'Arguments' tab, click on + and copy and paste this line there: ${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}
  6. click 'close' and run your program with your new scheme selected
  1. 而不是“编辑方案”点击“新方案”,给它一个名字并保存
  2. 现在选择您刚刚创建的新方案,然后单击“编辑方案”
  3. 转到“信息”选项卡,然后在“可执行文件”菜单中选择“其他...”
  4. 在文件窗口中,转到搜索输入字段并键入“终端”并在找到它时单击其图标。现在您应该在“可执行文件”字段中看到“Terminal.app”
  5. 转到“参数”选项卡,单击 + 并将此行复制并粘贴到那里: ${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}
  6. 单击“关闭”并选择新方案运行您的程序

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!

通常 Xcode 会为你打开终端。如果没有,您还可以关闭“信息”选项卡中的任何调试相关字段。希望这可以帮助!

Full tutorial here: https://www.raywenderlich.com/163134/command-line-programs-macos-tutorial-2

完整教程在这里:https: //www.raywenderlich.com/163134/command-line-programs-macos-tutorial-2

回答by Patrick

You can have the terminal launch everytime you run the application through editing the scheme. I believe this became available from Xcode 8.0.

您可以通过编辑方案在每次运行应用程序时启动终端。我相信这可以从 Xcode 8.0 中获得。

  1. Access Edit Scheme

    Edit Scheme

  2. Switch Consolefrom Use Xcodeto Use Terminal

    Console

  1. 使用权 Edit Scheme

    编辑方案

  2. 切换ConsoleUse XcodeUse Terminal

    安慰