Reputation: 326
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.
Upvotes: 3
Views: 866
Reputation: 385335
Because if file is .a file then I must be using static -linking and if file is .so I am using dynamic linking.
These filename "extensions" are only human conventions — there is no reason that they have to be .a
and .so
respectively. Quite rightly does GCC not force us into that convention.
Upvotes: 0
Reputation: 154017
I think you've got it backwards. Whether I'm linking with
a dynamic library or with a static one, under Windows, I need
a .lib
file; they just aren't generated in the same manner,
and don't contain the same thing. Under Unix, when I link with
a shared object, I link directly against the .so
file, and not
against some additional file generated at the same time.
The options -Bstatic
and -Bdynamic
only come into play when
both a .so
and a .a
are present, in the same directory; they
tell the linker which one to choose for libraries specified
using the -l
option. In Windows, this situation cannot occur;
since you need a .lib
for the .dll
, you cannot have a .lib
to be statically linked in the same directory.
Upvotes: 0
Reputation: 363807
In the GNU toolchain, and other Unix compilers, you generally don't specify the full path to a library; you give the linker a flag such as
-lfoo
and let it figure out whether it should link with libfoo.a
, libfoo.so
, and where these files are located. When only a static library is available, it will link with that.
So, you don't really need to specify -Bstatic
except in a few specific cases. Dynamic linking is the default and if you don't want a custom library to be linked dynamically, then just don't build a .so
out of it.
Besides, you can explicitly link with a static library by giving the full filename to the linker
gcc -o some_binary main.o libfoo.a
to get a dynamically linked binary with libfoo
statically linked in.
Upvotes: 0
Reputation: 129514
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.
Upvotes: 1