Running C scripts with terminal instead of Xcode

南楼画角 提交于 2019-12-01 01:15:12

HOWTO Pass command line arguments to your program from Xcode IDE

  1. Open your project's active Scheme. Easiest way to do this is from the main menu. Select Product/Scheme/Edit Scheme...
  2. In the schema editor, you'll see several build targets on the left side; the one you want is the "Run YourProjectName" target. Select it.
  3. On the right side you'll see four sub-tabs, including Info, Arguments, Options, and Diagnostics. Select Arguments
  4. Add/Remove any arguments you need. In your case, add /phi as the first argument, then 10 as the second.

Noteworthy: This is also where you can specify the current working directory of your program at launch rather than the long, temp path Xcode uses when building your binaries. To do so:

  1. Perform steps 1-2 from above.
  2. Select the Options sub-tab
  3. Click the "Use custom working directory" checkbox.
  4. Specify the full path where you want Xcode to execute your program from.

That in combination with getting your parameter handling fixed in your program should get you up and running.

Sounds to me like you want to get your arguments from the command line and if they are missing, prompt the user for them

Lets assume you want the arguments: number word number

#include <stdio.h>
#include <string.h>

int number1;
char word[128];
int number2;

int main(int argc, const char **argv)
{
    if (argc == 4)
    {
        number1 = atoi(argv[1]);
        strcpy(word, argv[2]);
        number2 = atoi(argv[3]);
    }
    else
    {
        do
            printf("Enter number word number: ");
        while (scanf("%d %s %d", &number1, word, &number2) != 3);
    }

    printf("I got %d '%s' %d\n", number1, word, number2);
    return 0;
}

Which gives:

$ ./test
Enter number word number: 1 andy 12
I got 1 'andy' 12
$ ./test 2 pandy 56
I got 2 'pandy' 56

Note that the error-checking is poor in this example and can be improved alot (not using atoi() is one way to start).

It sounds like you need to check argc in the program as RageD points out, otherwise when launching the program with insufficient arguments will cause problems.

gcc is the c compiler - it produces an executable. When you hit 'Run' in Xcode it compiles your program and then runs the executable file created. The executable created is named the same as your Xcode project name.

To run the program you built in Xcode from the command line:

  • In Xcode's project navigator find the executable in the 'Products' folder
  • Drag the executable file into Terminal (You will get an absolute url to the executable)
  • add any arguments you need to run your program
  • Hit enter!

The result will look something similar to the snippet below (for my 'MyCommandLineApp' project):

$ /Users/pliskin/Library/Developer/Xcode/DerivedData/MyCommandLineApp-hbpuxhguakaagvdlpdmqczucadim/Build/Products/Debug/MyCommandLineApp argument1 argument2
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!