11

I am new to Xcode subprojects. In my iPhone app project MyProject, I am trying to refactor some common code to a static library project called MyLibrary. After I create MyLibrary and move the code, MyProject is no-longer compiling. The error is that the MyProject cannot see the .h files in MyLibrary.

The error MyLibraryConfig.h: No such file or directory is coming in the line:

#import "MyLibraryConfig.h"
  • How to import the MyLibrary .h files in MyProject ?
  • What is the best practice here? Assuming I have multiple such libraries, it is tedious to add these to header search paths to the parent project.
Shameem
  • 14,199
  • 13
  • 40
  • 43

2 Answers2

28

In build settings --> Header Search Path --> Add below entry

$(SRCROOT) and mark it as recursive.

enter image description here

Saran
  • 6,274
  • 3
  • 39
  • 48
10

Maybe your header file is also in a subdirectory.

Imagine the following directory setup:

- Desktop
    - MyProject
        - MyProject.xcodeproj
        - main.m

        - MyLibrary
            - MyLibrary.xcodeproj
            - MyHeaderFile.h      <-- wanted header file

If main.m has these contents:

#include "MyHeaderFile.h"

int main() {
    return 0;
}

The compiler (gcc) will think that MyHeaderFile.h is located in the same directory as main.m, from which it is included. To tell the compiler you mean the header file in a subfolder, you have can do two things.

  • You can add a directory to the gcc compiler that says: "hey, also look in that folder". You can do this by using the -iquote myFolder flag.
  • You can include the directory in the include-statement: #include "MyLibrary/MyHeaderFile.h"

There could of course be another problem, but this seams like the most straightforward one.

v1Axvw
  • 3,054
  • 3
  • 26
  • 40
  • 2
    For the first bullet, that hey, also look in that folder setting is HEADER_SEARCH_PATHS in the Build Settings. – rooftop Feb 29 '12 at 15:37
  • @lef2, rooftop: Thanks for the answer. However, any easier way? Assuming I have multiple such libraries, it is tedious to add these to header search paths to the parent project. – Shameem Feb 29 '12 at 16:52
  • Ok, Header search paths has a recursive flag. This looks good. via http://www.cocoanetics.com/2011/12/sub-projects-in-xcode/ . – Shameem Feb 29 '12 at 17:10
  • What if I wanted to do the opposite? If I had a MyView.m file in MyLibrary and I wanted to import a file called Constants.h that was in the MyProject directory? – rob1302 Jun 23 '12 at 04:40
  • `#include "../MyHeaderFile.h"` should do the trick. Or `-iquote ..`. `../` is the UNIX alias to the parent directory. Of course you'll have to setup the flag for your library Xcode project instead of your other one. – v1Axvw Jun 23 '12 at 07:59