Suppressing Compile Time Linkage of Shared Libraries

Skip compile-time symbol resolution when building Linux shared libraries with dependencies

I think you're looking for --allow-shlib-undefined. From the ld man page:

--allow-shlib-undefined
--no-allow-shlib-undefined
Allows (the default) or disallows undefined symbols in shared libraries.
This switch is similar to --no-undefined except that it determines the
behaviour when the undefined symbols are in a shared library rather than
a regular object file. It does not affect how undefined symbols in regular
object files are handled.

The reason that --allow-shlib-undefined is the default is that the shared
library being specified at link time may not be the same as the one that
is available at load time, so the symbols might actually be resolvable at
load time. Plus there are some systems, (eg BeOS) where undefined symbols
in shared libraries is normal. (The kernel patches them at load time to
select which function is most appropriate for the current architecture.
This is used for example to dynamically select an appropriate memset
function). Apparently it is also normal for HPPA shared libraries to have
undefined symbols.

Allowing undefined symbols is the default, though, so I'm guessing your problem is actually something different.

Limiting visibility of symbols when linking shared libraries

GNU ld can do that on ELF platforms.

Here is how to do it with a linker version script:

/* foo.c */
int foo() { return 42; }
int bar() { return foo() + 1; }
int baz() { return bar() - 1; }

gcc -fPIC -shared -o libfoo.so foo.c && nm -D libfoo.so | grep ' T '

By default, all symbols are exported:

0000000000000718 T _fini
00000000000005b8 T _init
00000000000006b7 T bar
00000000000006c9 T baz
00000000000006ac T foo

Let's say you want to export only bar() and baz(). Create a "version script" libfoo.version:

FOO {
global: bar; baz; # explicitly list symbols to be exported
local: *; # hide everything else
};

Pass it to the linker:

gcc -fPIC -shared -o libfoo.so foo.c -Wl,--version-script=libfoo.version

Observe exported symbols:

nm -D libfoo.so | grep ' T '
00000000000005f7 T bar
0000000000000609 T baz

How do you suppress all 3rd party compile time warnings in MSVC

All this is from this blog post: https://devblogs.microsoft.com/cppblog/broken-warnings-theory/.
The general tone of the introduction of the article speaks volumes about why this option wasn't there to begin with (and none of it makes much sense to me).

Basically this says you can use /external:I as a synonym to -isystem.
You will probably need /external:templates- as well, due to the way MSVC handles warnings from templates.

Unfortunately, I cannot find any reference to any of this in the MSVC commandline documentation, nor the release notes related to the mentioned VS2017 15.6, so your mileage may vary.
There is though, a CMake issue requesting support for this feature behind their SYSTEM modifier.

Can I mix static and shared-object libraries when linking?

Looking at this thread you can see that it can be done. The guys at GNU suggest

gcc foo.c -Wl,-Bstatic -lbar -lbaz -lqux -Wl,-Bdynamic -lcorge -o foo.exe

Run-time linking to dynamic libraries not on LD_LIBRARY_PATH

You can either

  1. Write a wrapper script to always include LD_LIBRARY_PATH before calling the actual program (more flexible).
  2. Add -Wl,-rpath=<directory> to your linker options to add a directory to the runtime library search path. So assuming you have a libfoo.so and your program and DSO are located in the same directory, your compilation command could look like this: gcc -o myprogam main.c -L. -lfoo -Wl,-rpath='$ORIGIN'.

Update: As correctly noted by Maxim, setting -rpath=. is dangerous and should be avoided.

For -Wl,, see the gcc manpage and for -rpath see the ld manpage.

Selectively suppressing glibc link warnings?

is there any command-line switch that can be passed to ld or gcc in order to suppress it

No.



Related Topics



Leave a reply



Submit