[nSLUG] Static Linking Question C++

Jon Watson me at jonwatson.ca
Sat May 31 10:44:35 ADT 2008

Hi All,

While this is a programming question and not specifically a Linux question,
I think it's close enough to be in scope.

In a nutshell, I am having troubles understanding and implementing static
linking using g++. I haven't done anything with C++ since 1999 and my
knowledge ranges from rusty to never-even-knew-that and am hoping someone
with more experience can fill in some blanks for me.

I am writing an application that makes use of NetSeiben C++ SSH library (1).
It requires the botan library to handle the cryptographic stuff (2) and I am
having troubles linking to it. I'm also having conceptual problems
understanding why I have to compile using the command that I do.

Problem number one is the command I seem to have to use in order to compile
my application at all with the ne7ssh libraries. It only produces a working
executable of I compile using this command:

g++ foo.cpp -ofoo src/crypt.cpp src/ne7ssh_channel.cpp
src/ne7ssh_connection.cpp src/ne7ssh_error.cpp src/ne7ssh_kex.cpp
src/ne7ssh_keys.cpp src/ne7ssh_mutex.cpp src/ne7ssh.cpp
src/ne7ssh_session.cpp src/ne7ssh_string.cpp src/ne7ssh_transport.cpp
src/ne7ssh_sftp.cpp src/ne7ssh_sftp_packet.cpp -lbotan

While this isn't a huge problem, really, it looks and feels so clunky that I
am afraid I am doing something wrong that will later bite me in the butt. Is
there a better practice to achieve the same result?

Problem number two is that all of the standard libs and botan, are being
dynamically linked to my app and for reasons which are outside of the scope
of this email, we require static linking:

ldd foo
        linux-vdso.so.1 =>  (0x00007ffff37fe000)
        libbotan-1.6.3.so => /usr/lib/libbotan-1.6.3.so (0x00007f58eafdc000)
        libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00007f58eacd1000)
        libm.so.6 => /lib/libm.so.6 (0x00007f58eaa50000)
        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00007f58ea842000)
        libc.so.6 => /lib/libc.so.6 (0x00007f58ea4e0000)
        libpthread.so.0 => /lib/libpthread.so.0 (0x00007f58ea2c4000)
        libbz2.so.1.0 => /lib/libbz2.so.1.0 (0x00007f58ea0b4000)
        libcrypto.so.0.9.8 => /usr/lib/libcrypto.so.0.9.8
        libgmp.so.3 => /usr/lib/libgmp.so.3 (0x00007f58e9af5000)
        librt.so.1 => /lib/librt.so.1 (0x00007f58e98ec000)
        libz.so.1 => /usr/lib/libz.so.1 (0x00007f58e96d5000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f58eb442000)
        libdl.so.2 => /lib/libdl.so.2 (0x00007f58e94d1000)

The problem is that I cannot figure out how to statically link the botan
library. I have done considerable Googling and it seems that the solution is
to generate an archive .a file using the ar application and include all of
the botan object files in the archive. I found a botan.a file in my /usr/lib
directory an attempted to recompile using the -static switch and appending
/usr/lib/libbotan.a to the end of the compile command, but g++ throws a
bunch of undefined references to the botan library which seems to indicate
it is not being included properly. An 'ar -t libbotan.a' shows a long list
of .o files contained in the archive which is what I expect.

So I guess my question is that assuming this .a file is what I need in order
to statically link the botan library to my application, what do I do with
it? I can't seem to find the proper way to include it in the compile command
so I'm left not sure if I need to include it in one of the other cpp files
or something else. This is the part of my knowledge that is deep into
never-even-knew-that territory.

Can someone point me in the right direction? Links, advice, tips all



1. http://netsieben.com/products/ssh/
2. http://botan.randombit.net/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://nslug.ns.ca/mailman/private/nslug/attachments/20080531/288fea23/attachment-0001.html>

More information about the nSLUG mailing list