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!
Let me guess: your other file contains a
mainfunction. A project is not allowed to have multiplemainfunctions (in fact, all non-static functions must have unique names). Themaindoes not need to be in themain.cfile, but there needs to be exactly onemain.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.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.cfile.