14

This is the point. I have a subproject (static library) created. The library compiles well. No errors. When I include this library inside a project and imports a header from that library on the project the library fails to compile because it cannot find a path that belongs to itself.

After following a bunch of tutorials on the web on how to create a static library and embed that in a project, I don't know which one is the correct one, because I have tried all and all failed and some differ.

This is how the library is set:


STATIC LIBRARY

BUILD SETTINGS:

  1. Public header folder path = $(BUILT_PRODUCTS_DIR)
  2. Header search path = $(SRCROOT) (recursive)

BUILD PHASES

COPY FILES = 1 file, myLibrary.h that is basically empty (created by xcode when I used the static library template to start the library.

no ADD COPY HEADERS phase


MAIN PROJECT

BUILD SETTINGS

  1. Header search path = empty
  2. User header search path = $(BUILT_PRODUCTS_DIR) (recursive)
  3. Always search user paths = YES

BUILD PHASES

  1. Yes, myLibrary.a is on target dependencies

What amazes me is that the library compiles fine alone but when put inside a project, is unable to find a header that belongs to the own library.

NOTE: I have also tried to create a copy headers phase on the library making public all .h on that library, but it failed too.

This is an example of one error:

/Users/mike/Library/Developer/Xcode/DerivedData/MyApp-dnaimaovscreasadhmvgttwjnabc/Build/Products/Debug-iphoneos/include/myLibrary/ccTypes.h:39:9:
fatal error: 'Platforms/CCGL.h' file not found
#import "Platforms/CCGL.h"

I have lost two days trying to solve this nightmare.

any thoughts?

I have uploaded a sample project to here and here

Regexident
  • 29,441
  • 10
  • 93
  • 100
Duck
  • 34,902
  • 47
  • 248
  • 470
  • Add `Run Script` build phase to be executed before the failing phase, check `Show environmental variables in build log`, start the build and check the paths at `Log Navigator`. – A-Live Apr 14 '13 at 17:53
  • and the script is empty? Just that? Doing this shows nothing more that what was already shown on log navigator. – Duck Apr 14 '13 at 18:00
  • I have uploaded a sample project to here: https://mega.co.nz/#!mBQhiRQS!Mz0wRh9wdhbKsW_KxE5F8iL2Q11gocuHlAB0ibQOoDo so you can see what I mean. – Duck Apr 15 '13 at 14:30
  • It says Safari is outdated (sic) and then asks to accept some kind of terms of use, could you please upload the project to a bit more friendly host ? I'm going to check it later today. – A-Live Apr 15 '13 at 14:46
  • 1
    No problem. Here we go. Please try **[this](http://dfiles.eu/files/vj7egy9ig)** or **[this](http://www.sendspace.com/file/dfvs09)** – Duck Apr 15 '13 at 17:33

5 Answers5

12

After downloading your sample project, I had it working in a few minutes by making the following changes.

First, you need to add an entry for the MyProject target's Build Settings under Header Search Paths, so that the files such as HelloWorldLayer.h, which #import "cocos2d.h", know where to find that file.

Under the Header Search Paths, I entered ../MyStaticLibrary/MyStaticLibrary/libs/** like shown in the image below:

enter image description here

That solved the problem of the inability of the preprocessor to find the necessary source files from the MyStaticLibrary, for the MyProject project, but after compiling, I got an error about missing symbols. I needed to add the built libMyStaticLibrary.a to the Link Binary With Libraries step like shown in the image below:

enter image description here

After that, the project compiles fine.

NSGod
  • 22,699
  • 3
  • 58
  • 66
3

(Not enough rep to post comment...)

Did you try specifically including the Platforms directory in the header search path? Presumably, Platforms is in the source directory, not in $(BUILT_PRODUCTS_DIR) so may not be searched in your current setting.

Vivek
  • 564
  • 4
  • 13
  • have you tried the sample project I have uploaded? See the final paragraph of my question. Thanks. – Duck Apr 15 '13 at 21:15
  • @RubberDuck I believe his idea is the same as mine: adding correct headers search path. You might also play with `Build Locations` settings to get the build files of libs and main project be at the same root products dir. – A-Live Apr 16 '13 at 13:42
  • BTW, I have tried your answer and unfortunately it is not working for me. I am about to give up building a static library on Xcode. It is another shame for Apple that Xcode is that bad. Thanks for your effort. – Duck Apr 16 '13 at 18:20
  • I have found a solution for this question. Thanks for the effort. You were almost there but there are other points involved. See my answer. Thanks. – Duck Apr 16 '13 at 20:28
3

Regarding script to show you env variables, here's how I do it (open images at the new tab for better scaling):

  • Added dummy shell script

enter image description here

  • Observed its output at Log Navigator

enter image description here

enter image description here

enter image description here


As you can see, BUILT_PRODUCTS_DIR doesn't have any headers copied from the library. Either put headers there manually (strongly not recommended) or add search path to the location that you know the headers must be at:

enter image description here enter image description here

Now as there were two headers cocos2d.h and MyStaticLibrary.h, cocos2d.h was successfully imported although it will have additional dependency.

The path ../MyStaticLibrary/build/$(BUILD_STYLE)-$(PLATFORM_NAME) will also (recursively) have public headers of the library.

A-Live
  • 8,904
  • 2
  • 39
  • 74
  • Thanks. I will try that later. BTW, apple must be kidding with developers. – Duck Apr 16 '13 at 14:37
  • @RubberDuck Why, did you follow any guide from developer.apple.com and it wasn't working ? I would guess it happened because of lack of documentation from cocos2d or whatever lib you wanted to use, Xcode does its job pretty well. – A-Live Apr 16 '13 at 14:54
  • I don't think so. Xcode 4 is a piece of crap. It crashes several times for the the stupid reasons, like removing an image file a project is using by deleting it using finder. Yes, I have followed the guide from Apple and also about 10 guides from other sites on the web. The problem is not with Cocos. Xcode must find any path of anything it compiles that is imported on the project. It is like importing an image on a text editor, seeing that image there, inserting it on text, format everything ok and when you want to print the document the editor tells you the image cannot be found. – Duck Apr 16 '13 at 15:29
  • 1
    There are clearly two Apples out there. One for consumers: I love that Apple. One for developers that is light-years behind the loved one. – Duck Apr 16 '13 at 15:31
  • @RubberDuck Lets try to make Xcode better: there's a feedback address and bug-report service at [Xcode Release Notes](https://developer.apple.com/library/mac/#releasenotes/DeveloperTools/RN-Xcode/). I'd personally love to see Xcode adding search-paths from dependent projects and mark them somehow as generated automatically, but it wasn't really as critical for me. By the way, they've released an update yesterday, maybe your crashes with images have been already analyzed and fixed. If no, it will most probably help if you leave a bug report or just send reports from standard error popup window. – A-Live Apr 16 '13 at 15:40
  • I have about 30 bug reports/feature requests about Xcode there, submitted since the first day the infamous Xcode 4 was released. These were almost 3 years sending bugs to them. Never had any feedback on any. I estimate that 20% of these bugs I have filled have been partially fixed. Some others and some feature requests unfortunately would require redesigning Xcode 4, that stinks (infinity + 1)^2. I think I am not alone hating Xcode 4. [this site](http://amplicate.com/hate/xcode) pulled a poll that returned with 62% of people hating Xcode. Just google for "hate xcode 4"... – Duck Apr 16 '13 at 18:14
  • BTW, I have tried your answer and unfortunately it is not working for me. I am about to give up building a static library on Xcode. It is another shame for Apple that Xcode is that bad. Thanks for your effort. – Duck Apr 16 '13 at 18:17
  • 1
    I've just quickly (took a minute or two) added `$(SRCROOT)/../MyStaticLibrary/libs/cocos2d` and `$(SRCROOT)/../MyStaticLibrary/` to (user and system) header search path, both recursive, added your library to linking stage (it was missing), enabled `always search user path` and it builds just fine. The lib project is a bit messy, I think if you try to organize it better (maybe with sub-libraries) it will be easier to maintain. – A-Live Apr 16 '13 at 18:45
  • I have uploaded a 2.0 version of the sample project a few moments ago to here: http://www.sendspace.com/file/79as8c. On this second version I have split Cocos to be another library. Anyway can you upload the project you have? – Duck Apr 16 '13 at 19:02
  • I have found a solution for this question. Thanks for the effort. – Duck Apr 16 '13 at 20:27
2

After almost 5 days of a nightmare trying to solve that, I finally found this tutorial: http://www.youtube.com/watch?v=XUhLETxgcoE

This is the only solution that worked for me. The simplest and the best.

Thanks for every one trying to solve this.

Duck
  • 34,902
  • 47
  • 248
  • 470
0

Please try this…

step1: right click on the 'projectName.xcodeproj'(static library).Choose 'Get info' and copy the full path from(General->where)

step2: Paste full path to 'Header search paths' for your main project(Both Targets and Project )

IKKA
  • 6,297
  • 7
  • 50
  • 88