17

Some libraries require the -all_load linker flag when linking to an Xcode project. However, this leads to a linker error if there are symbol conflicts among libraries. The solution is to use -force_load, which effectively lets you use -all_load on some libraries, but not on others.

However, this in turn leads to a new problem, at least for me. Whenever I use -force_load with a relative path to a library, the linker always finds symbol conflicts between the library and itself. It appears that the linker thinks that the library with its absolute path and the library with its relative path are different libraries, and therefore finds conflicts between the library and itself.

I can avoid this by using an absolute path with the flag. But this is not a wonderful solution, as it is convenient to keep source code for libraries within my documents directory. But the path to the documents directory will be different on other machines.

Question: Can anyone get force_load to work with a relative path to the library?

EDIT: for background information, see this question

Community
  • 1
  • 1
William Jockusch
  • 26,513
  • 49
  • 182
  • 323

2 Answers2

37

With Xcode 4, if you include the library project into your app project, then you can add this to the Other Linker Flags:

-force_load $(BUILT_PRODUCTS_DIR)/<library_name.a>

You still need the dependency, and you need to add the library in the Link Phase list of frameworks and libraries too.

EDIT: Apple now says as of some Xcode 4 release that you can simply use this linker flag: "-ObjC" to get libraries with categories to properly load. That flag is working just fine for me in Xcode 5. People are still up voting this answer, but I suspect that the -ObjC flag is the best solution now.

David H
  • 40,852
  • 12
  • 92
  • 138
  • @AlexanderVasenin that is quite a statement, you should back it up with links to posts here supporting it. Personally I never heard this until now. – David H Dec 17 '14 at 13:09
  • Examples: [link 1](http://www.personagraph.com/ios#faq19), [link 2](https://www.parse.com/questions/linker-flag-objc-causes-build-to-fail) – Alexander Vasenin Dec 17 '14 at 14:44
  • @AlexanderVasenin the first link is innuendo (hearsay), the second is an actual problem. But the problem (IMHO) is not a problem at all. Someone wants to link a library in which has a dependency on another library. The user doesn't want the dependent library, so wants to avoid the link error and proceed as if there was none. I would view this as a poor decision since how can the developer know for sure that other library is not ever going to get messaged (and thus result in a user app crash)? In the end this is a developer decision, but not a bug or deficiency in the Apple -Objc flag. – David H Dec 17 '14 at 14:52
  • 2
    David, your answer is right, and I actually upvoted it, but I found it while searching how to get rid of -ObjC flag that broke some poorly written code I have no intention to rewrite. – Alexander Vasenin Dec 17 '14 at 15:25
  • @AlexanderVasenin and I thank you for that! I just want to insure that future readers of this post know the full story! Best wishes! – David H Dec 18 '14 at 02:43
  • @DavidH Agree with your statement that its not a deficiency in Apple -Objc Flag. There are techniques like reflection to support optional dependency on other libraries. Just to clarify for future readers, the [link1](http://www.personagraph.com/ios#faq19) is not a hearsay. We found that few libraries (including the one in [link 2](https://www.parse.com/questions/linker-flag-objc-causes-build-to-fail)) which had the issue when -ObjC flag was used. Hence we wanted to provide alternative suggestion of using force_load for those who are encountering the linker errors when -ObjC flag was used. – Bharath Booshan Feb 21 '15 at 19:54
  • @BharathBooshan thank you for the clarification - sure other readers will appreciate it! – David H Feb 22 '15 at 12:37
8

This worked for me. Like the above answers you still need to include the library in the project.

-force_load $(SRCROOT)/pathToLibraryFromProject/libname.a

For the path it's just the folders in your project that lead to where you put your library, for example BaseFoler/Subfolder/libName.a.

Theris10
  • 81
  • 1
  • 1
  • This worked for me. Entering just the relative URL didnt work for me. so I had to enter the absolute URL for it to work. This is on Xcode 8.3, Swift 3 – Karthik Kannan May 05 '17 at 09:05