How to link Boost libraries statically, but with dynamic runtime?
I know there are some messages on this subject, but none of them gave me the answer or then I just didn't understand it. I'm trying to link some boost libraries statically to my program, but at the same time link the standard libraries dynamically. I have little experience ( == a total noob) in the linking process. I'm using ld version 2.16.1 and g++ version 4.1.2 on Linux. My /usr/lib has files like libboost_regex.so libboost_regex-s.a (the same for multi-threaded progs) Actually I'm unable even to link everything statically: ld <obj-files> -Bstatic -lboost_filesystem-s -lboost_program_options-s -lboost_regex-s -o prog fails. Also trying to link runtime dynamically with ld <obj-files> -Bstatic -lboost_filesystem-s -lboost_program_options-s -lboost_regex-s -Bdynamic -o prog fails. I also tried to ar/ranlib the so object files into .a archives (not sure if it can help in any case) and then link them as above, but it didn't work either (similar linking errors). Linking everything dynamically works, but I want to learn how to link libraries statically. Thanks, niko
Niko Vuokko wrote:
I know there are some messages on this subject, but none of them gave me the answer or then I just didn't understand it. I'm trying to link some boost libraries statically to my program, but at the same time link the standard libraries dynamically. I have little experience ( == a total noob) in the linking process. I'm using ld version 2.16.1 and g++ version 4.1.2 on Linux. My /usr/lib has files like
libboost_regex.so libboost_regex-s.a (the same for multi-threaded progs)
Actually I'm unable even to link everything statically:
ld <obj-files> -Bstatic -lboost_filesystem-s -lboost_program_options-s -lboost_regex-s -o prog
You better avoid calling ld directly, unless you're certified ld guru. Use g++ instead.
fails.
Since you failed to provide the error message, the above is all I can suggest.
Also trying to link runtime dynamically with
ld <obj-files> -Bstatic -lboost_filesystem-s -lboost_program_options-s -lboost_regex-s -Bdynamic -o prog
fails.
First, you fail to provide the error message again. Second, which manual say that using trailing -Bdynamic works? Further, with explicit '-lboost_filesystem-s' you don't need -Bstatic.
I also tried to ar/ranlib the so object files into .a archives
That will never work. .a archives can technically contain anything from .o to .jpg, but only .o will be used by linker. So: (1) use gcc/g++, not ld and (2) provide the real error message. - Volodya
Vladimir Prus wrote:
You better avoid calling ld directly, unless you're certified ld guru. Use g++ instead.
Dude, I feel like a massive idiot right now. At first I had tried to use g++ like: g++ <objs> -Wl,-Bstatic,-libboost_regex-s,... -o prog but it didn't work out so I tried to give the commands straight to ld. I picked up that -Wl,-Bstatic from some group. I was just amazed that none of the programming courses I've been to have ever said half a word about linking and all its possibilities. I now tried to dump that -Wl,-Bstatic because it gets sent to ld: g++ <objs> -llibboost_regex-s ... -o prog and it just worked. Thanks for that ld -> g++ tip, I'm just embarrassed I didn't try that earlier. By the way, do you know any good books or sites where I could read about object file structures, linking and all that? Something more extensive than just a short tutorial would be nice. (Yes, it's offtopic, but you're probably reading this anyway and seem to know these things.) niko
Niko Vuokko wrote:
Vladimir Prus wrote:
You better avoid calling ld directly, unless you're certified ld guru. Use g++ instead.
Dude, I feel like a massive idiot right now. At first I had tried to use g++ like:
g++ <objs> -Wl,-Bstatic,-libboost_regex-s,... -o prog
but it didn't work out
because I *think* you'd need two -Wl options to achieve that effect.
so I tried to give the commands straight to ld. I picked up that -Wl,-Bstatic from some group. I was just amazed that none of the programming courses I've been to have ever said half a word about linking and all its possibilities.
I now tried to dump that -Wl,-Bstatic because it gets sent to ld:
g++ <objs> -llibboost_regex-s ... -o prog
and it just worked. Thanks for that ld -> g++ tip, I'm just embarrassed I didn't try that earlier.
By the way, do you know any good books or sites where I could read about object file structures, linking and all that? Something more extensive than just a short tutorial would be nice. (Yes, it's offtopic, but you're probably reading this anyway and seem to know these things.)
I'm afraid I don't know. On Unix, object files are in ELF format, and you can read about it in System V ABI document: http://www.sco.com/developers/gabi/2003-12-17/contents.html but it's not an introduction document, and is not oriented at end users, more at folks who have a need to directly work with object files. - Volodya
niko
Yeah, you're right: g++ <objs> -Wl,-Bstatic,-lboost_filesystem-s -Wl,-Bstatic,-lboost_program_options-s -Wl,-Bstatic,-lboost_regex-s -Wl,-Bdynamic -o prog seems to work. Note that you need that -Wl,-Bdynamic there, otherwise ld starts to look for lgcc_S.
Well, it's not the most noob-friendly text, but it gives all the ideas, thanks. niko
Niko Vuokko writes:
By the way, do you know any good books or sites where I could read about object file structures, linking and all that? Something more extensive than just a short tutorial would be nice. (Yes, it's offtopic, but you're probably reading this anyway and seem to know these things.)
The book "Linkers & Loaders" is rather nice: http://www.amazon.com/Linkers-Kaufmann-Software-Engineering-Programming/dp/1... -bryan
Niko Vuokko writes:
By the way, do you know any good books or sites where I could read
object file structures, linking and all that? Something more extensive
about than
just a short tutorial would be nice. (Yes, it's offtopic, but you're probably reading this anyway and seem to know these things.)
The author also offers the book in manuscript form (aka unedited) for free on the web. I read this book back in college, when I also felt in the dark about how exactly object files and linkers work. While interesting, you are probably not going to feel a whole lot more comfortable working directly with the linker or understanding often cryptic link errors. Link here: http://www.iecc.com/linker/ A decent article with a good amount of depth can also be found at Linux Journal: http://www.linuxjournal.com/article/6463 -K
Stevens, Kevin wrote:
The author also offers the book in manuscript form (aka unedited) for free on the web. I read this book back in college, when I also felt in the dark about how exactly object files and linkers work. While interesting, you are probably not going to feel a whole lot more comfortable working directly with the linker or understanding often cryptic link errors. Link here: http://www.iecc.com/linker/
A decent article with a good amount of depth can also be found at Linux Journal: http://www.linuxjournal.com/article/6463
A huge thanks for these! It's not about being a better debugger, but about understanding the ideology behind object files and such. But on the other hand, I guess it WILL help in the deciphering of errors, because then I won't be so prone to do stupid commands that are obviously wrong when you understand the fundamentals. niko
participants (4)
-
Bryan Green
-
Niko Vuokko
-
Stevens, Kevin
-
Vladimir Prus