A portable way to statically link to one of the libraries
I am creating a utility that depends on libassuan
aside from others. While these "others" provide shared libraries, it libassuan
comes with static only.
libassuan
comes with a simple tool libassuan-config
that is meant to provide CFLAGS
and LDFLAGS
use the compiler / linker. These LDFLAGS
refer to the library as -lassuan
.
The result of a standard make call:
cc -I / usr / include / libmirage -I / usr / include / glib-2.0 -I / usr / lib64 / glib-2.0 / include -lmirage -lglib-2.0 -L / usr / lib64 -lassuan -o mirage2iso mirage2iso. c mirage-getopt.o mirage-wrapper.o mirage-password.o mirage-password.o: In function `mirage_input_password ': mirage-password.c :(. text + 0x1f): undefined reference to `assuan_pipe_connect ' mirage-password.c :(. text + 0x32): undefined reference to `assuan_strerror ' collect2: ld returned 1 exit status make: *** [mirage2iso] Error 1
(I just started writing this unit and why there are no more errors)
So, if I understand the result correctly, gcc doesn't want to link the application to libassuan.a
.
Using -static
here will cause gcc to prefer static libraries over shared libraries, which are not inherent. I've seen a solution suggesting to use something like this:
-Wl,-Bstatic -lassuan -Wl,-Bdynamic
but I don't think it would be portable.
I think the best solution would be to provide the full path to the static library file, but libassuan-config
doesn't provide most of the help (all I can get from it is -L/usr/lib64 -lassuan
).
Maybe I'll just try to create a static library path by "parsing return LDFLAGS
and using -L
for the directory name and -L
for the library name), and then hoping that in all cases, libassuan-config
return it like this.
What do you think about this? Is there a good, simple and portable solution to solve the problem?
PS. Note that while I mean gcc here, I would like to use something that works great with other compilers.
PS2. Another question: if a package only installs a static library, would it be considered an error to return that LDFLAGS
instead of the full path .la
?
source to share
gcc will link to libassuan.a if it doesn't find libassuan.so
Probably order symbols are viewed in the static library when linking. The order matters. ) Assuming gcc can find libassuan.a and it actually provides the functions that the linker is complaining about, try:
cc -I / usr / include / libmirage -I / usr / include / glib-2.0 -I / usr / lib64 / glib-2.0 / include -lmirage -lglib-2.0 -L / usr / lib64 -o mirage2iso mirage2iso.c mirage -getopt.o mirage-wrapper.o mirage-password.o -lassuan
Since you say libassuan is under / usr / lib 64, it is probably a 64-bit library, as well as your application, as well as other 64-bit libraries?
source to share
Compiler command line options are not portable. There is no standard for this. Each compiler uses its own, and some may just informally agree to abide by each other in a command line format. The most portable way for your connection is to use of libassuan-config
course. I think it can generate not only flags for gcc, but also for other compilers. If it can't, then no portable path exists I suppose (other than CMake or something at a higher level).
The command line you selected is cc
completely correct. If you have a static library libassuan.la, and the path to it is provided in -L
, then the compiler will link to it. You can see this from your output: did he find the static library, would he regret an error like "cannot find -lassuan". I
Also, if libassuan.so is not found, then the compiler links to your library statically, even if you didn't use -Wl,-Bstatic
stuff or -static
.
Your problem might be keeping multiple versions of libassuan on your system. The other is that, I don't see any errors in what you provided.
source to share