简体   繁体   English

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

[英]Xcode, how to run one file at a time?

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. 我一直收到这个错误:“链接器命令失败,退出代码为1”,我注意到当我在多个文件中使用“int main”方法时会发生这种情况。 Furthermore, when I try to run a simple return 0 file, my "hello world" file still gives results to the console. 此外,当我尝试运行一个简单的返回0文件时,我的“hello world”文件仍然会向控制台提供结果。 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"). 所以我的假设是xcode同时运行项目中的所有文件,导致我在重复方法名称(例如“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. 顺便说一句,这些是C文件,我在下学期之前使用xcode作为练习我的c编程的工具。 Thanks for your help! 谢谢你的帮助!

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 membership for your source files. 您需要为每个程序创建单独的目标,并为源文件分配目标成员资格 Go to File->New->Target to create a new target. 转到File->New->Target以创建新目标。 From the sounds of it, you are creating a command-line C program, so you will want to create a Command-Line Tool found under OS X-> Application . 从它的声音来看,您正在创建一个命令行C程序,因此您需要创建一个在OS X-> Application下找到的Command-Line Tool

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). 作为另一种选择,假设您正在创建命令亚麻工具,如果您希望您只能将Xcode用作编辑器并从命令行构建程序(无论如何您可能必须为您的类创建)。 You can do this by creating your .c files and opening them in Xcode. 您可以通过创建.c文件并在Xcode中打开它们来完成此操作。 Save the files to the same folder. 将文件保存到同一文件夹。 To compile from the commandline, run something like the following in Terminal: 要从命令行进行编译,请在终端中运行以下内容:

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 tools are installed. 如果这不起作用,请确保已安装命令行工具

You can only have one int main per 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调用它。

I ran into this issue myself following Learning C by Dan Gookin with exercise files. 我在Dan Gookin学习C后用练习文件自己遇到了这个问题。 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. 我了解到,为了能够运行.c文件,必须创建一个项目,然后必须添加指定没有目标成员资格的文件,否则Xcode会将整个文件编译为三个可执行文件,并且遇到的错误会阻止程序跑步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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