Manik Sidana
Manik Sidana

Reputation: 2153

Difference between "dynamically loading a library file" and "specifying .so path in Makefile"?

I recently came across a code which loads a .so file with dl_open() and works with dlsym() etc. I understand that dl_open() this would load the dynamic library file. What is the difference between dynamically loading a library file and specifying .so path in Makefile?

Another question is that if I want to dynamically load a library file, do I need to compile it with -rdynamic option?

Aren't both these compiled with -fPIC flag?

Upvotes: 2

Views: 568

Answers (2)

cdarke
cdarke

Reputation: 44414

If you statically link an .so file in your Makefile then you won't be able to build the application unless it is present. It has the advantage of no nasty surprises at run time.

When creating a shared object, assuming you are using gcc, then -fpic only means the code can be relocated at run-time, you need -shared as well. I don't know the -rdynamic option, but compilers differ.

Loading the module at run-time allows the module load to be optional. For example, say you have a huge application with 300 modules, each representing different functionality. Does it make sense to map all 300 when a user might only use 10% of them? (code is loaded on demand anyway) It can also be used to load versions from different libraries at runtime, giving flexibility. Downside is that you can end-up loading incompatible versions.

Upvotes: 1

nav_jan
nav_jan

Reputation: 2553

Dynamically loading a library file is frequently used in implementing software plugins. unlike specifying .so path in Makefile or static linking, dynamic linking will allow a computer program to startup in the absence of these libraries, to discover available libraries, and to potentially gain additional functionality.

link

Upvotes: 1

Related Questions