0

I have seen the answer to my question heavily implied but have not seen it somewhere written, hence I am making these questions (just to be sure I am correct).

To use a shared library in a C/C++ program you need both the .h file and the .so file? (if I understand correctly, the compiler needs the .h file and the linker needs the .so file)

As a bonus question, is this true for static (.a) libraries?

Thanks in advance

Metalzero2
  • 531
  • 2
  • 6
  • 17
  • You need headers to see the declarations, a static stub library for the linker, and a dynamic library that can be accessed and loaded at runtime. – πάντα ῥεῖ May 22 '19 at 17:29
  • 1
    Short answer: Yes (and for both dynamic and static libraries). – Steve Summit May 22 '19 at 18:05
  • 1
    Your conclusion is almost correct. The compiler uses the declarations in .h to set up the calling structure, but you if you know them you can simply declare them yourself. :-)... Now when the compiler outputs the .o for a .c or a .cc you still have stubs (except the call stack for function calls is relatively okay). When the linker gets invoked, all .o and .a and even the .so (dynamic linking at runtime) resolve the "relative addresses" to actual addresses in memory of the final "linked executable" as it's loaded by the kernel upon `exec` using symbol lookups and other techniques. – Ahmed Masud May 22 '19 at 18:07
  • 1
    @πάνταῥεῖ a static stub library is as far as I know only needed on windows. – HAL9000 May 22 '19 at 18:08
  • @AhmedMasud thanks for the clear explanation. It was very useful :) – Metalzero2 May 23 '19 at 09:08

0 Answers0