3

I'm building my iOS project with linking errors. My project links against some third-party static libraries. They use different standard c++ library, one is libstdc++, the other is libc++.

Link against either one will cause the linking errors in other lib, so I link both of them in my target, the command line is -lstdc++ -lc++.

Now it build successfully, but I wonder if it will cause some runtime error, can anybody explain about this ? Thanks in advance.

KudoCC
  • 6,912
  • 1
  • 24
  • 53

1 Answers1

4

No, it is not safe. In fact, it is a direct violation of the One Definition Rule (ODR). The ODR says, among other things, that you can have at most one definition of any non-inline function in an entire program. You will violate this rule by linking two different implementations of the standard library.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Can you give an example about under what condition it is not safe. It's some kind of hard work to uniform the two library, they are from third-party, so if it won't come with terrible problems, I prefer to link both of them. – KudoCC Jan 12 '15 at 02:11
  • An example of why it's not safe: your program might allocate a resource using one of the implementations and release it using the other. Or one of the implementations might unknowingly call into the other. Or the two implementations may use the same name for two "different" global variables or class statics, and interfere with each other. It is *not safe* and *must not be done*, full stop. If you link both libraries, your program may exhibit *undefined behavior*. – John Zwinck Jan 12 '15 at 02:15
  • I'm curious to know why no duplicated symbol error when link both of them. Do the two library have different name of allocate and release method, if it does, I'm safe I think. :) – KudoCC Jan 12 '15 at 02:20
  • You're not safe. Please stop trying to hand-wave this problem away. Maybe you can explain the original reason why you got into this situation and we can work on that in a separate question? This might be helpful too: http://stackoverflow.com/a/8457799/4323 – John Zwinck Jan 12 '15 at 02:25
  • Thanks, I'll try my best to avoid the double-linking :) – KudoCC Jan 12 '15 at 02:34