How can an autotools user specify a combination of static & dynamic linking?
You did not specify the operating system, but let us assume that it is a relatively recent Unix/Linux/OSX. If it is not, then ignore the following warning.
Before I answer, you should know that mixing static and shared code on most ELF-based systems (Unix/Linux) is problematic. One reason is because it can lead to code getting out of sync if you forget to relink an updated dependency. Another is due to the nature of static code vs. PIC. This is why libtool tries to discourage it.
That being said, in the Makefile.am (assuming your final program is foo
and the shared library is):
.
.
.
foo_SOURCES = foo.c abc.c def.c hij.c
foo_LDFLAGS = -all-static -Wl,-Bdynamic,-L/path/to,-lshared,-Bstatic
foo_LDADD = -L../path/to -lbar -lbaz
What is important here is that libtool allows you to short-circuit the checks and GNU gcc's -static
flag (which is used by libtool) by passing -Wl,
arguments directly to the linker (GNU ld). To put spaces between arguments, the comma ,
delimiter is used.
Both -Bstatic
and -Bdynamic
are documented in GNU ld's info pages as well as the help screen. Again, since you did not mention the os or compiler package being used, I am assuming GNU gcc and GNU ld on Linux. You may want to verify by using ld --help
to see for yourself. If, for some reason, it is not GNU ld, then you will need to find the equivalent flags to -Bstatic
and -Bdynamic
, substituting where appropriate.