Iducool
Iducool

Reputation: 3663

Remove static library based on certain preprocessor macro

I have one functionality and it is depending on one static library. Actually, Apple is not allowing this static library because of few reasons. So, for now we have decided to add one Preprocessor macro and based on that functionality should be switch on/off.

I have switched my code to behave based on that Preprocessor macro. But I didn't find any to link or unlink the library based on preprocessor macro. I can not allow to link the library all times, as I have already told apple is not allowing it.

One solution that I already know : I can create the multiple target. But problem is that my app has already lots of targets. So, again to manage this I have to create one more target for each target those are already created.

Any help will be appreciated.

Edit:

I have created configuration like Francesco suggested. But One quick question I have let's say If I will remove the path of library from "Library Search Path" then that library will not be linked to app? Because that library is still in target of App.

Upvotes: 2

Views: 563

Answers (3)

Iducool
Iducool

Reputation: 3663

Credit of this answer goes to Nikolai Ruhe & Francesco

I have combined the answer of both and I got the solution.

Here are the steps that I followed.

  • Created new configuration. ( To know how to create configuration see the answer of Francesco)

  • Added flag EXCLUDED_SOURCE_FILE_NAMES in user-defined setting of Build settings. (For steps see the answer of Nikolai Ruhe). And in this flag I have added the name of my static library under the my custom configuration.

Here is a good tutorial that will definitely help you : Remove tesflight from Distriubtion

Upvotes: 1

Francesco
Francesco

Reputation: 1848

Instead of targets you can create multiple configurations. And you can change the linker flags there. I did this think for Sparkle framework (which is not allowed on App Store)

EDIT:

To create/manage the configurations click on the project in the sidebar. Then in the main window select again the project, not the targets, and select the "Info" tab.

Together with Deployment Target selection and Languages you will find a Configuration section. You can add or remove configurations from there. Project config

Then to launch it you have to go to Product -> Scheme -> Edit Scheme -> Build Configurations. You can duplicate an existing scheme and choose the correct configuration from there.

schemes

Upvotes: 3

Nikolai Ruhe
Nikolai Ruhe

Reputation: 81878

You can remove the static library from the list of linked items by setting a (somewhat undocumented) Xcode build setting:

EXCLUDED_SOURCE_FILE_NAMES = libUnwanted.a

You can do this in an .xcconfig file or in the normal target build settings UI (by adding a custom build setting). If you for example set this in the "Release" configuration Xcode will include the library in the "Debug" build but omit if for the release build.

EXCLUDED_SOURCE_FILE_NAMES works with all kinds of files that can be added to a target: source files, resources, libraries, ...

Here's how to do this step by step:

  1. Open the target build settings by clicking on the blue project icon in the Project Navigator and select the Build Settings tab.
  2. Click the plus icon to add a "User-Defined Setting"
  3. Name the setting "EXCLUDED_SOURCE_FILE_NAMES"
  4. Expand the Configurations for the setting and set the "Release" value to the name of your unwanted library.

Upvotes: 1

Related Questions