Xcode,如何一次运行一个文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17910856/
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-15 03:38:14  来源:igfitidea点击:

Xcode, how to run one file at a time?

cxcodefile

提问by user2251001

Xcode Question, how to run one file at a time?

Xcode 问题,如何一次运行一个文件?

I keep getting this error: "Linker command failed with exit code 1" and I've noticed that it occurs when i use the "int main" method in multiple files. Furthermore, when I try to run a simple return 0 file, my "hello world" file still gives results to the console. So my assumption is that xcode is running all of the files in the project at the same time causing me to have a duplication error when I repeat method names (such as "int main"). How do I only run one file at a time? These are C files by the way, I'm using xcode as a tool to practice my c programming before next semester. Thanks for your help!

我不断收到此错误:“链接器命令失败,退出代码为 1”,我注意到当我在多个文件中使用“int main”方法时会发生这种情况。此外,当我尝试运行一个简单的 return 0 文件时,我的“hello world”文件仍然向控制台提供结果。所以我的假设是 xcode 同时运行项目中的所有文件,导致我在重复方法名称(例如“int main”)时出现重复错误。如何一次只运行一个文件?顺便说一下,这些是 C 文件,我使用 xcode 作为工具在下学期之前练习我的 C 编程。谢谢你的帮助!

回答by BergQuester

It sounds like you're trying to build multiple programs. The problem is you have a single "target" which is what Xcode uses to define what goes into an application.

听起来您正在尝试构建多个程序。问题是你有一个单一的“目标”,这是 Xcode 用来定义进入应用程序的内容。

You will need to create a separate target for each of your programs and assign target membershipfor your source files. Go to File->New->Targetto create a new target. From the sounds of it, you are creating a command-line C program, so you will want to create a Command-Line Toolfound under OS X-> Application.

您需要为每个程序创建一个单独的目标,并为源文件分配目标成员资格。去File->New->Target创建一个新的目标。从它的声音来看,您正在创建一个命令行 C 程序,因此您需要Command-Line ToolOS X-> Application.

Alternatively, you could also create separate projects for each program. See File->New->Program

或者,您也可以为每个程序创建单独的项目。看File->New->Program

As yet another alternative, assuming you are creating command-linen tools, if you wish you can use Xcode merely as an editor and build the program from the commandline (which you may have to do for your classes anyway). You can do this by creating your .c files and opening them in Xcode. Save the files to the same folder. To compile from the commandline, run something like the following in Terminal:

作为另一种选择,假设您正在创建命令行工具,如果您希望可以仅将 Xcode 用作编辑器并从命令行构建程序(无论如何您可能必须为您的类执行此操作)。您可以通过创建 .c 文件并在 Xcode 中打开它们来完成此操作。将文件保存到同一文件夹中。要从命令行编译,请在终端中运行类似以下内容:

gcc -Wall file1.c file2.c -o myprogram

You would then run your program by giving:

然后,您可以通过以下方式运行您的程序:

./myprogram

If that doesn't work, make sure the command line toolsare installed.

如果这不起作用,请确保安装了命令行工具

回答by Mohamad Ali Baydoun

You can only have one int mainper target.

int main每个目标只能有一个。

You're getting linker errors because you're defining more than one.

您收到链接器错误是因为您定义了多个。

If you need to run a function other than main at the start of execution, call it from main.

如果需要在执行开始时运行 main 以外的函数,请从 main 中调用它。

回答by evalenzuelaa

I ran into this issue myself following Learning C by Dan Gookin with exercise files. I learned that in order to be able to run the .c files, a project must be created then the files must be added specifying no target membership, otherwise Xcode will compile the entire files three as one executable and the errors encounter would prevent the program to run.

在 Dan Gookin 使用练习文件学习 C 之后,我自己遇到了这个问题。我了解到为了能够运行 .c 文件,必须创建一个项目,然后必须添加指定没有目标成员资格的文件,否则 Xcode 会将整个文件三个编译为一个可执行文件,遇到的错误将阻止程序跑步。