XCode importing a header file contained in a folder

Discussion in 'iOS Programming' started by zmeiapas, Feb 18, 2010.

  1. zmeiapas macrumors newbie

    Feb 18, 2010
    Hi there guys,

    I am new to XCode development (and to the Mac world in general) and I just encountered a very simple problem that I can't figure out the answer to.

    Could someone tell me how do you go about importing header files contained in a folder? So let's say you create a new project, and you decide to import the header file in your app's viewController using the following line

    #import "MyFolder/headerFile.h"
    where MyFolder is obviously the name of the folder that contains the header file.

    I tried creating different groups and then adding the folder containing the header file to those groups using both options "Recursively create groups for any added folders" and "Create folder reference for any added folder" but no matter what I tried the header file would always be imported properly only by

    #import "headerFile.h"
    rather than

    #import "MyFolder/headerFile.h"
    The only workaround I found was to basically make my file structure flat which is plain retarded. I'm sure I'm missing something but can't figure it out for several hours now :confused:

    Your help would be very appreciated!

    Cheers! :apple:
  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    If you add a header file to a project, which is recommended, then you use a simple #include with no path in it.

    The #includes with a path in them are referred to as framework-style includes and are used when the headers are inside a framework. None of your class headers will be in frameworks.

    If you use some open-source code that uses #includes with a path in them you probably have to monkey with the header search path setting.
  3. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    Groups are like virtual folders that only exist within Xcode. They don't create actual folders within your application bundle. To do that, follow these steps: 1) right click on the Classes group and choose Add > Existing Files... 2) pick the folder you want to add and click Add, 3) Make sure you have "Copy items into destination's group folder" option checked as well as "Create Folder References for any added folders" selected. Once you add the folder, in Xcode it should look blue instead of yellow. Now when you build, that same directory should exist in the app structure.
  4. Gepper macrumors member

    Dec 7, 2009
    Unless my memory is all wrong this is incorrect.
    #include <whatever> will try to include file 'whatever' by looking at all include paths defined by your system. #include <folder/whatever> will do the same, but looks for a folder named 'folder' in the same global paths.
    #include "file" will look in the current directory.

    #include and #import are pretty much equal, except import will make sure each file is only included once.

    Correct me if i'm wrong ;-)
  5. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    What part of what I said is incorrect?
  6. Guiyon macrumors 6502a

    Mar 19, 2008
    North Shore, MA
    Actually, if I recall correctly, the search order is something like this:

    1. Search the current directory
    2. Search any directories specified by -iquote<dir> flag
    3. Follow angle bracket search order
    Angle brackets:
    1. Search any directories specified by -I<dir> flag (in order)
    2. Search any system directories
    An example of how this effect compilation is:
    #include "stdlib.h"
    #include <stdlib.h>
    Both of these includes have the exact same effect unless you have a header called "stdlib.h" either in GCC's working directory or in a directory specified by the -iquote flag. In that case, the "" include will use the local file and <> will use the system one.

    These are standard includes, there is nothing special about them; they just specify a path from one of the system include directories instead of a header directly in the search path. They existed long before Apple adopted them for frameworks and it's just a simple way of namespacing header files. Take a look at either the sys/* headers or openssl/* headers for standard libraries that use that conventions.
  7. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Yes. They are relative paths. Apple uses those for what it calls framework-style includes. Things like:

    #import <Foundation/Foundation.h>

    where the root directory of the framework headers is implicitly set as a search path by Xcode or by gcc. If you open up Foundation.framework you might be surprised to find that the headers aren't in a directory named Foundation.

    And yes it's also true that these kinds of relative paths have been in use in *nix-land for a long time. That's why I said:

    Because the root directory that gcc requires to find the header referred to by the relative path is probably not in the search paths that Xcode sets up implicitly for you when you add a header to your project.
  8. Gepper macrumors member

    Dec 7, 2009
    I'm not sure what kind of stuff Apple does with their SDK, but it's rather an exception then a rule. <blah.h> are for system wide files, "bla.h" for files local to your project. Unless your project includes a library that you have installed system wide :)

Share This Page