Static Library path nightmare from hell

2020-06-01 08:25发布

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

5条回答
Summer. ? 凉城
2楼-- · 2020-06-01 08:45

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.

查看更多
混吃等死
3楼-- · 2020-06-01 08:54

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.

查看更多
乱世女痞
4楼-- · 2020-06-01 08:56

(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.

查看更多
萌系小妹纸
5楼-- · 2020-06-01 09:03

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.

查看更多
Summer. ? 凉城
6楼-- · 2020-06-01 09:12

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 )

查看更多
登录 后发表回答