Is it possible to use a static library only when t

2019-03-13 23:15发布

问题:

I have external closed library that can compile with armv7s (etc) only. when I try to compile against simulator obviously its not running and display some errors. I don't want to insert this library to my code unless I can configure Xcode to use this library only when i test with a device. unfortunately, i tried to do it with cocoapods with no success and i wonder if there any way to do it?

回答1:

Yes, this can be done. I had a similar problem with a framework that caused linker errors only in the simulator so I setup up my project to only use the framework when building for a device.

The following assumes you are not using cocoa pods to link the library. I'm not sure what would need to be changed if you are.

  1. Select your target and go to the Build Phases tab.
  2. Under the "Link Binary With Libraries" section, remove the static library from the list.
  3. Go to the Build Settings tab.
  4. Find the "Other Linker Flags" setting.
  5. Double-click on the Debug value. Tap the + and enter -lsomelibrary
  6. In place of "somelibrary" enter the actual name of your library minus the leading "lib". Do not include the extension.
  7. Select the Debug value and notice a little circled +. Click the +.
  8. Click on the new "Any Architecture | Any SDK" part and change it to "Any iOS Simulator SDK".
  9. Now double click on the value to the right of "Any iOS Simulator SDK" and remove the -lsomelibrary entry you added.

Now do a debug build.

The above change basically means that the library is linked in for all builds except for iOS Simulator builds.

You will probably also need to make some code changes. Any code making any reference to header files or other symbols from the library should be wrapped as follows:

#if !TARGET_IPHONE_SIMULATOR
#import "somelibrary.h"
#endif

#if !TARGET_IPHONE_SIMULATOR
    // Use stuff from the library
#endif


回答2:

The reason is because the Library is missing the i386 (simulator) architecture slice, not a problem...

  1. Add your library to the project
  2. Wrap all your references to the library, including imports, in preprocessor directives for Debug vs Production. Preprocessor Directives to separate targets in xcode
  3. Manage your Schemes so that Simulator is Debug and Build (to Device) is set for Production.
  4. create a duplicate build target... One for simulator, and one for Device/production http://blog.just2us.com/2009/07/tutorial-creating-multiple-targets-for-xcode-iphone-projects/
  5. in build settings remove the library from Link Binary With Libraries for the "simulator" target

Now, when you use the "Sim" target... library and all related code will be excluded.

When you use the "Device" Target... the code and library will be included



回答3:

Steps to avoid the simulator problem with static library without simulator architecture:

  1. Select the framework like optional in in Link Binary With Libraries.
  2. Access to Build Settings to Linking part and add in Debug "-lNameLibrary". Then, check the button plus and select the option Any iOS Simulator SDK like key. Delete de value information add in the value information in the row Any iOS Simulator SDK.
  3. Add this code in the places where you use the library.
#if targetEnviroment(simulator)
#else
#endif