Search code examples
c++botan

Botan AutoSeeded_RNG can not be initialized, unknown reference, although libs are correctly linked


I simply try to initialize the Botan AutoSeeded_RNG but it fails because of a bad reference. I just wanted to test if i can initialize any kind of botan RNG, corse i have trouble with it in another project.

I have the correct header included and am linking to the lib of Botan, therefore i don't know, why it can not find the referende.

Here is my code:

  1 #include <botan/auto_rng.h>
  2 #include <botan/ecdh.h>
  3 #include <botan/ec_group.h>
  4 #include <botan/pubkey.h>
  5 #include <botan/hex.h>
  6 #include <iostream>
  7
  8 int main() {
  9
 10     Botan::AutoSeeded_RNG rng;
 11
 12     return 0;
 13 }
 14

And here is my output:

~/projects $ g++ ecdh.cpp -o ecdh -I/usr/local/include/botan-2/ -L/usr/local/lib/
/usr/bin/ld: /tmp/cccPpNuZ.o: in function `main':
ecdh.cpp:(.text+0x18): undefined reference to `Botan::AutoSeeded_RNG::AutoSeeded_RNG(unsigned int)'
/usr/bin/ld: ecdh.cpp:(.text+0x28): undefined reference to `Botan::AutoSeeded_RNG::~AutoSeeded_RNG()'
collect2: error: ld returned 1 exit status

What am i doing wrong?

Thx for your advice in advance.


Solution

  • You are not linking against the botan library. The -L <dir> flag only adds the directory to the library search paths, it does not tell g++ to link against any specific library. To link against a library, you have to use the -l <lib> parameter. The linker will then search for this library in its library search paths, including the directories passed with -L <dir>.

    To link against the botan library, you have to find the directory containing the botan library. I understand in your case this is /usr/local/lib/libbotan-2.so. You would then add the -lbotan-2 parameter to the g++ parameter list. This will make g++ look for libraries called libbotan-2.so in its library search paths. Since you added /usr/local/lib to the library search paths with the -L /usr/local/lib parameter, g++ should then be able to locate the library in this folder.

    Note that additional parameters may be required or are recommended by the botan library for an optimal experience. You can find these parameters in a file called botan-2.pc, which should be contained somewhere in your custom installation. On my system, it contains the following information:

    $ cat /usr/lib/pkgconfig/botan-2.pc
    prefix=/usr
    exec_prefix=${prefix}
    libdir=/usr/lib
    includedir=${prefix}/include/botan-2
    
    Name: Botan
    Description: Crypto and TLS for C++11
    Version: 2.15.0
    
    Libs: -L${libdir} -lbotan-2 -fstack-protector -m64 -pthread
    Libs.private: -lbz2 -ldl -llzma -lrt -lz
    Cflags: -I${includedir}
    

    This information can also be queried directly using the pkg-config command:

    $ PKG_CONFIG_PATH=/some/path:$PKG_CONFIG_PATH pkg-config --libs --cflags botan-2
    -I/usr/include/botan-2 -lbotan-2 -fstack-protector -m64 -pthread
    

    Where /some/path is the directory containing the botan-2.pc. This gives us the recommended compiler and linker flags for the botan library. We could either copy them to the g++ parameter list manually, or pass them automatically using:

    g++ $(PKG_CONFIG_PATH=/some/path:$PKG_CONFIG_PATH pkg-config --libs --cflags botan-2) my_program.cpp