问题描述
我正在 Xcode 4.4.1 中编写 C 程序.一个新项目以一个 main.c 文件开始,这显然是我的主文件.如果我想编写不同的程序怎么办?如果我选择 File>New File,Xcode 会在 main.c 旁边创建一个新的 .c 文件.问题是,当我将代码放入那个新文件中时,当我尝试运行它时,我会收到与链接有关的构建错误.我总是可以从 program2 中获取代码并将其粘贴到 main.c 文件中的一个全新项目中,它会起作用,但是,出于组织目的(大学工作),我更愿意在同一个项目中有多个文件可以独立编译和执行.我对 Xcode 很陌生,所以我问,我做错了什么/不理解?
I'm writing a C program in Xcode 4.4.1. A new project starts me off with a main.c file which is obviously my main file. What if I want to write a different program? If I choose File>New File, Xcode creates a new .c file next to main.c. The trouble is that when I place code in that new file, when I attempt to run it, I get a build error having to do with linking. I can always take the code from program2 and paste it into a brand new project in the main.c file and it will work but, for organization purposes(college work), I'd prefer to just have multiple files in the same project which can be independently compiled and executed. I'm very new to Xcode, so I ask, what am I doing wrong/not understanding?
我有照片要发布,但由于我刚刚注册了此帐户,因此无法发布.提前致谢!
I have pics to post but I'm not able due to my just having registered this account. Thanks in advance!
推荐答案
让我猜猜:您的另一个文件包含一个 main
函数.一个项目不允许有多个 main
函数(实际上所有非静态函数都必须有唯一的名称).main
不需要在 main.c
文件中,但必须只有一个 main
.
Let me guess: your other file contains a main
function. A project is not allowed to have multiple main
functions (in fact, all non-static functions must have unique names). The main
does not need to be in the main.c
file, but there needs to be exactly one main
.
一种解决方案是将其他文件中的函数重命名为其他文件(main2
、main3
等).这将阻止链接器抱怨.
One solution is to rename the functions in other files except the one that you want to run to something else (main2
, main3
, and so on). This will stop the linker from complaining.
然而,这种方法容易出错.标准方法是使用多个项目——您编写的每个程序一个.为方便起见,您可以将它们保存在同一个 Xcode 工作区中,但每个都应该有一个单独的目标和自己的 main.c
文件.
However, this approach is error-prone. The standard approach is to use multiple projects - one for each program that you write. You can keep them in the same Xcode workspace for convenience, but each one should have a separate target and its own main.c
file.
这篇关于Xcode 4.4.1 - C 编程 - 添加新的 .c 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!