CocoaPods / CocoaPods

The Cocoa Dependency Manager.
https://cocoapods.org/
Other
14.57k stars 2.63k forks source link

linker errors from using -ObjC #712

Closed deanmao closed 9 years ago

deanmao commented 11 years ago

I noticed that CocoaPods will auto inject -ObjC into the linker flags for your project. This flag causes all symbols to be loaded and may cause errors with other existing frameworks that may not like this flag. The fix would simply be to use a more "targeted" flag as described in Apple's QA 1490: http://developer.apple.com/library/mac/#qa/qa1490/_index.html

Basically, the fix would be to use something like this as the linker flag instead: -force_load $(TARGET_BUILD_DIR)/libPods.a

This would have the same desired effect as -ObjC without any of the undesirable side effects.

fabiopelosin commented 11 years ago

There have been some issues that led to the use of the -ObjC solution. However I've not been involved with them and I'm not familiar with the reasons that led to the current implementation. I need to dig the issue. However in the meanwhile it would be great if you could provide a concrete example of the issues that you have been experiencing.

deanmao commented 11 years ago

Hi yes, I made a blog post about it: http://www.deanmao.com/2012/12/31/linker-error-using-cocoapods/

The short story is that the official opencv ios framework has a few missing symbols in their library, but these symbols are almost never accessed so it will never cause a runtime exception. Using -ObjC forces the linker to load all frameworks and check for missing symbols so the linking step will fail. The simple fix is make libPods.a be fully loaded while leaving everything else alone.

fabiopelosin commented 11 years ago

I see, thanks for the detailed report.

alloy commented 11 years ago

Awesome report, @deanmao, thanks!

fcy commented 11 years ago

My 2 cents in this is that the -force_load should be used specific to each pod that needs it.

I'm having problems using the Parse SDK without the Facebook SDK with CocoaPods because of this. I manually removed the -ObjC flag all together and it fixed the parse-pod-without-facebook issue but I've other pods that needs the flag and those started to give me errors.

fabiopelosin commented 11 years ago

Did you try to replace the -ObjC flag with -force_load?

fcy commented 11 years ago

Do you mean -force_load only libPods.a? it wouldn't work because everything (Parse and Facebook) are pods.

hramos commented 11 years ago

I'm working on providing an official CocaPods podspec file for Parse, and running into this too. The Facebook SDK shouldn't be required, but CocoaPods injects -objc which would require us to add the Facebook SDK to the podspec as a dependency unnecessarily.

knshiro commented 10 years ago

I managed to make the Parse pod work without facebook dependency by:

I'm not expert in Xcode projects builds but it seems that one way to make this work would be to add an option in the Podspec to leave the library generated by the pod outside of the libPods.a and of course replace -ObjC by -force_load $(TARGET_BUILD_DIR)/libPods.a in Pods.xcconfig

knshiro commented 10 years ago

I kept digging a bit and I guess what @fcy was proposing is not ideal for CocoaPods since it will mean putting more dependencies between the xcodeproj and the xcworkspace...

Anyway I added this in my Podfile as a dirty fix to do what I was doing manually before except that I need to add manually once libPods-Parse.a

The first part is pretty awful and I would suggest that it would be replaced in CocoaPods source directly.

post_install do |installer_representation|

   # Replace -ObjC by -force_load
   path = installer_representation.sandbox_root + 'Pods.xcconfig'
   f = File.open(path, "r")
   g = File.open(path.to_s + '.tmp', "w")
   f.each_line do |line|
     line.sub! '-ObjC', '-force_load $(TARGET_BUILD_DIR)/libPods.a'
     g.puts line
   end
   f.close
   g.close
   File.rename(g.path, f.path)

   # Remove target dependency from Pods target
   installer_representation.project.targets.each do |target|
     target.dependencies.select!{|d| d.target.name != 'Pods-Parse' }
   end

 end
CocoaPodsBot commented 10 years ago

Issue has been confirmed by @joelparsons

fatuhoku commented 10 years ago

This is an incredibly long running issue. Any updates?

wilmarvh commented 10 years ago

@fatuhoku I ended up using the @knshiro's suggestion. Works great, but not ideal.

segiddins commented 10 years ago

I think we can close this one out.

segiddins commented 9 years ago

@alloy ping?

alloy commented 9 years ago

@segiddins Yeah I think so too, especially wit frameworks on the horizon.

MattHillebrandHTC commented 9 years ago

I'm sorry, but why was this issue closed? It's affecting me severely, even with the latest CocoaPods v0.37.1.

I've got some C++ libraries that simply don't jive with the -ObjC linker flag.

segiddins commented 9 years ago

@MattHillebrandHTC please open up a new issue that includes a sample project?

MattHillebrandHTC commented 9 years ago

It's the same issue... I also don't think my company would like me posting their C++ libraries publicly :)

segiddins commented 9 years ago

well without a sample project, we're not going to be able to track this down. Doesn't have to be your company's lib, but we need something we can run to see the problem in order to fix it,

MattHillebrandHTC commented 9 years ago

With or without CocoaPods, I can't use the linker flag "-ObjC" in my project. If I use "-force_load " for each static library individually (like everyone else has been saying in this thread), I can link...but that's only if I don't use CocoaPods. Is there a way for me opt-out of CocoaPods applying "-ObjC"?

neonichu commented 9 years ago

I would have recommended using frameworks to integrate, but apparently we are still supplying -ObjC there, I think that is definitely superfluous. I have created #3537 to change that.

MattHillebrandHTC commented 9 years ago

Thank you!

aanchalkalani commented 8 years ago

Hi,

We are getting errors even after adding libPods.a in other linker flag,

Targets in Build Settings :- Other Linker Flags :- Remove -ObjC and add -force_load $(TARGET_BUILD_DIR)/libPods.a but after adding the above line we are getting below errors :-

ld: file not found: /Users/abc/Library/Developer/Xcode/DerivedData/MyApp-cti.........yl.........ifas/Build/Products/Debug-iphonesimulator/libPods.a
clang: error: linker command failed with exit code 1 (use -v to see invocation)

And our Pods.xcconfig file is lock we can't able to edit -ObjC by -force_load $(TARGET_BUILD_DIR)/libPods.a in Pods.xcconfig

Any help appreciated