Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why explicit parameters for static and dynamic linking

I have mostly worked in Windows, and recently I started working in Linux. I have a doubt. I have used Visual Studio as IDE in Windows and used Makefile in Linux.

There are two types of libraries in Windows (VC++), static library (.lib) and DLL. It is quite obvious (isn't it?) if I link with lib file I am using static linking else dynamic linking.

Now when I use g++ compiler, why I need to explicitly mention -Bstatic/-static or Bdynamic/-dynamic flags. Because if file is .a file then I must be using static -linking and if file is .so I am using dynamic linking.

like image 271
Pranit P Kothari Avatar asked Jun 01 '26 14:06

Pranit P Kothari


1 Answers

There are times when you want to "force" the compiler to do what it normally wouldn't. In particularly -static is useful when you are building against a library that may not be installed [or not have the same version installed] on another machine where you want your code to run.

the -Bdynamic is useful if you want to have ONE library linked statically, but not EVERY library that your code uses.

e.g. gcc -o myprog myprog.o -Wl,-Bstatic -lspecial -Wl,-Bdynamic will link myprog using static linking for libspecial (which may be something that isn't widely distributed, for example something you have built yourself)

For general, local development, you don't need either.

like image 104
Mats Petersson Avatar answered Jun 04 '26 09:06

Mats Petersson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!