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

1
  • I need $(SRCROOT) otherwise I get duplicated symbols
    – onmyway133
    Aug 23, 2018 at 9:48

2 Answers 2

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.

7
  • @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, 2014 at 13:09
  • Examples: link 1, link 2 Dec 17, 2014 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, 2014 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. Dec 17, 2014 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, 2014 at 2:43
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.

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 May 5, 2017 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.