Just start with Xcode 4.3

Discussion in 'Mac Programming' started by benthicwhale, Jul 30, 2011.

  1. benthicwhale macrumors newbie

    Joined:
    Jul 30, 2011
    #1
    Hi, guys I just begin learning how to program in Xcode 4.3. And I am attending class of C program in university, so I use Xcode Command Line to run some simple C programs.

    My operation is as follows: Run Xcode 4.3 - Create a new Xcode project -
    Application (under 'Mac OS X' menu) - Command Line Tool. Then I copied content of *.c to Main.c in the new project, and then build and run in Main.c.

    My question is, when I add a new .c file to this project, I can not build Main.c successfully. what should I do to add new .c files associated with Main.c into the project?

    Please help me, thank you very much! :confused:
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    Unless you have a super awesome access to Apple internally released code, you can't have XCode 4.3. ;) You might want to check again was version of XCode you actually have.

    What error messages are you getting in Main.c when you add other .c files to your project?
     
  3. benthicwhale thread starter macrumors newbie

    Joined:
    Jul 30, 2011
    #3
    Ok, thank you for your reply, maybe it is not version of 4.3, but should be 4.*. When I added another .c file to my project, I just could not build successfully my project, always built failed.

    My question is how to associate another .c file with main.c?

    Thank you very much.
     
  4. ehoui macrumors regular

    Joined:
    Jan 27, 2011
    #4
    Is this a new game show? How much do I win for guessing the answer correctly?
     
  5. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #5
    You need to tell us what you tried and specifically what errors you're getting. Otherwise it's very difficult for us to help you.
     
  6. kingofqueens macrumors newbie

    Joined:
    Aug 28, 2009
    #6
    his question is a very straight forward one?


    To include a file to the main you use something like this

    Code:
    #include "stack.h"
    <-stack.h holds all the prototypes for stack.c which gets called from stack.h thru main.c

    ask me here if you need more help!


    Happy coding =)
     
  7. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #7
    Possibly. Well have to wait and see if you're assumption is correct.
     
  8. benthicwhale, Aug 4, 2011
    Last edited by a moderator: Aug 4, 2011

    benthicwhale thread starter macrumors newbie

    Joined:
    Jul 30, 2011
    #8
    Yeah, your assumption is right, that is my straightforward question. Create a new project, then there will be a 'main.c' in the project. I want to add another 3 '.c' files into this project. But after adding another one .c file in the project. I can not build for running successfully. Even though I add '#include <name.c> in main.c. I still built failed.

    Because one project can only have one main function. So I need to change the name of main() in other .c files(except main.c). And then invoke this function in main.c. Is that right?

    But after I adding a new .c file into the project, such as name1.c, and including this name1.c in main.c by '#include <name1.c>', I received a error message of 'name1.c file not found'. Why?
     
  9. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #9
    Firstly, typically you don't #include .c files. Typically you #include .h files. Header files (.h files) only include a declaration/prototype of the functions defined in the corresponding .c file. If you #include a .c file and that .c file is also in your project, you will get duplicate symbol errors when your program is linked because both the included .c file and including .c file will end up having the same code. You'll want to create a header file for each .c file except for the one containing main. You don't need to create declarations for all the functions, just the opens you want call from other .c files.

    Secondly, you want to #include "name1.h" (with double-quotes) not #include <name1.h> (with angle-brackets). The former is used to include header files local to your project. The later is used to include header files from the library header search path (typically /usr/include).
     
  10. benthicwhale thread starter macrumors newbie

    Joined:
    Jul 30, 2011
    #10
    Thank you very much, jiminaus, for your kind and patience. My question is quite simple, because I am a Xcode beginner. Now I have figured out the solution of my own question. I share it with our friends in MacRumors.

    First, I must clarify that it is not version 4.3, it's 4.1.

    Then essence of my question is that Xcode can not find customised files no matter .h file or .c file in my SSD. The reason of it is that we indeed need to set some options in the Xcode Project Setting.

    Do it as follows: Select my project name (such as Lab_02 1target, Mac OS X SDK 10.7) - then a middle list will appear with two options of 'PROJECT' and 'TARGETS' - choose one of them arbitrarily - on the right list where you can see 'info' and 'Build Settings' buttons in the top menu, choose 'Build Setting' button - a submenu appears under the top menu - confirm that 'ALL' and ' Combined' options are highlighted ( Default setting is 'Basic' ) - in the Setting list, find 'Search Paths' - build 'Always Search User Paths' to 'yes' ( default is no ) - Add a path to 'Header Search Paths' ( Such as: /Users/benthicwhale/Documents/Xcode_Projects/Header_Files). I should make sure that there is no blank space in this path!! - Then back to my main.c build for running again to see whether Xcode has found this .h file according to the new search path.

    Hope this solution can help you with your Xcode career.

    Thank everyone who replied my question!
     
  11. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #11
    If you had of used #include "abc.h" instead of #include <abc.h> you wouldn't have had to do this.
     
  12. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #12
    Just FWIW this is not an Xcode problem. It is a C problem.

    benthicwhale, you might want to consider learning C independent of the IDE by using gcc/clang from the command line instead. This gets the IDE out of the way and may be closer to what your class does.

    Are they using Linux or Windows in class?

    B
     
  13. benthicwhale thread starter macrumors newbie

    Joined:
    Jul 30, 2011
    #13
    Thank you, you are right, with "include" I just need to add reference of .h or .c files into my project, then everything works smoothly.

    Thank you again.
     

Share This Page