-2

I know that passing objects to/from DLLs can be unsafe, but I am unsure if the same is true for static libraries.

Is it safe to pass objects, such as STL objects, to and from a static library?

cowlinator
  • 7,195
  • 6
  • 41
  • 61
  • Show some [MCVE] and tell where you read that passing objects to DLLs is unsafe (since that is wrong) – Basile Starynkevitch Mar 07 '18 at 03:33
  • @BasileStarynkevitch: I believe the standard issue is if the main executable and the DLL have a different definition of the C or C++ runtime. See [example C problem here](https://stackoverflow.com/q/19712236/364696) (and sorta related [C++ problem](https://stackoverflow.com/q/2322095/364696), though I think it that case it wasn't a conflicting runtime issue). – ShadowRanger Mar 07 '18 at 03:35
  • But that is not DLL specific. Same issue with a static library or some outside object file. – Basile Starynkevitch Mar 07 '18 at 03:40
  • 1
    All 4 answers of this question state that passing objects to/from a DLL is unsafe. https://stackoverflow.com/questions/22797418/how-do-i-safely-pass-objects-especially-stl-objects-to-and-from-a-dll . See also https://social.msdn.microsoft.com/Forums/vstudio/en-US/d8e73f43-a652-414a-89d9-ae032dec1b3f/how-to-communicate-usermade-object-across-dll-interface?forum=vclanguage – cowlinator Mar 07 '18 at 20:23

1 Answers1

2

It better be. A static library is essentially just a bunch of compiled object files mushed together and indexed to simplify linking; if it wasn't safe to pass STL objects to a static library, it wouldn't be safe to pass them between functions defined in separate .cpp files at all.

Obviously, you need to make sure the static library was compiled with a compiler using the same ABI as the one you eventually compile and link your own code with, but there isn't the issue with linking different runtime libraries that occurs with dynamic linkage.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271