Simon
Simon

Reputation: 537

Xcode: Multiple versions of a static library

I'm trying to create a project in Xcode (6.0.1) that uses two versions of a static library. Therefore I have created a directory structure that looks similar to this:

+ include
  |-- VersionA
      |-- foo.h
      |-- bar.h
  |-- VersionB
      |-- foo.h
      |-- bar.h

+ lib
  |-- VersionA
      |-- foo.a
      |-- bar.a
  |-- VersionB
      |-- foo.a
      |-- bar.a

The headers foo.h and bar.h in the both directories are similar, whereas the executables are different. In order to include a certain header, I (non-recursively) added the the include directory to the search path an included like this:

#include "VersionA/foo.h"   // or this:
#include "VersionB/bar.h"

Now the question: How does Xcode decide which Mach-O fat (.a) file to link for a certain header file? How does this assignment work, and how can I influence it?

EDIT:

I get no duplicate symbol linking errors, although the linker looks into both lib directories. Since the header files have include guards like

#ifndef FOO_H
#define FOO_H
...

I also get no errors regarding redefinition. If I change one include guard i.e.

#ifndef FOO_H_VERSION1

there are errors at compile time (Sematic Issue, Redefinition of 'FOO_SOMETHING'). This makes sense to me and indicates that it might not be best practice to duplicate header files.

Nevertheless, for my project I need two versions of a static library. What would be a reasonable way to achieve this?

Upvotes: 1

Views: 1629

Answers (2)

ttarik
ttarik

Reputation: 3853

Including a header file does not automatically include or link a library - you indicate which libraries you'd like to be linked in the "Link Binaries with Libraries" section of your Build Phases.

In your case, you will simply need to add all of the libraries and then use the corresponding header file where relevant.

However, you've said that these are different versions of the same library. If your libraries contain methods of the same name, the linker will complain about that. With this in mind, you should reconsider whether or not it is seriously necessary to include separate versions of the same library - if there is an issue with one/both of the libraries that's stopping you from using just one, it may be more useful (and less stressful, ultimately) to try and resolve that problem.

Upvotes: 2

borrrden
borrrden

Reputation: 33421

Header files are not "linked" to anything. Here is what happens. You create a promise for a whole bunch of different methods via headers, and then give that promise to the linker. One of the linker's jobs is to search all of the artifacts that you have included in your project to make sure that each of those methods is actually there. If one is missing, you will get a missing symbol error. If both libraries define a method with the same name, you will get a duplicate symbol error. In the end those .a files don't exist. Everything is put together into one binary file.

Upvotes: 0

Related Questions