XCode 4.2 and Using a Static Library Causing Probl

2020-02-04 02:32发布

问题:

Like many others here SO, I am having issues using libraries in my XCode workspace. The idea is I have my main project (MyApp) and two libraries (lib1 and lib2) which were added to the workspace after the code was finalized.

For background, lib1 is dependent on lib2, since there are some protocols that need to be implemented in order to make use of lib2. MyApp is dependent on both. I have tried following the Apple guides, which implies what I would like to is simple. After that, I have followed pretty much every post here on SO that has come up in my searches (2 days worth of Googling), plus many other blogs and posts that claim to have solutions, many of which are XCode 3 specific.

Yet, my project does not build. I am using Xcode 4.2 on OS 10.6 (upgrading to Lion is not an option). Is there a simple tutorial to follow on how to create a library project, and use this library in an application.

I came across this answer while writing this question (where the question had the same error I had), which I will look into, but I have reached the point of copying source files over just to get a demo working.

UPDATE:

Here is list of issues that have come up. I will add more when they arise:

  • **directory not found for option '-L/Users/myhomedir/iphoneprojects/MyApp/../BLProfile/build/Release-iphoneos'**

回答1:

Here is how to create a static library and a project that use it.

Step 1: Create a workspace: File > New > Workspace. This is not mandatory but it helps organize the projects.

Step 2: Create a static library project: File > New > Project > Framework and Library > Cocoa Touch Static Library. Lets call it MyLib.

Step 3: Create an Application Project: File > New > Project > Application> Empty Application, or any other application template. Lets call this project MyApp. If you placed the projects in different folders, you may have to use a different path.

Now you have the following Projects in the workspace.

Step 4: Configure the app so that it finds the .h files of the library. To do that, add the parent folder "../" and check the recursive checkbox.

Step 5: Link the app to the library.



回答2:

  1. Open Main Project where you would like to add static library.
  2. Save as workspace.
  3. Add library's project package. No need to check copy while adding project package.
  4. Update Xcode preferences location to Custom-Relative to Workspace. Products as 'Build/Products' and Intermediates as Build/Intermediates
  5. In your Main project -> target -> build settings, Add Header and Library Search Path as $(BUILD_PRODUCTS_DIR)
  6. Build Library Project. So a new library will be created in mentioned build path.
  7. Remove existing library from Build Phases and move to trash. Open Build Phases -> Link Binary With Libraries and add newly created library from build directory.


标签: xcode