0

I started working with c++ a few weeks ago, and I started a new project to start learning more. I'm encountering an issue with an external dependency. I'm trying to use a library called: libbgp, and I installed it base on their documentation. Here is my code: https://gist.github.com/amb1s1/9b2c72294da0ec9416810c8686d3adce

Error:

/usr/bin/ld: /tmp/ccsdO32O.o: in function `ticker(libbgp::BgpFsm&)':
ambgp.cpp:(.text+0xa7): undefined reference to `libbgp::BgpFsm::tick()'
collect2: error: ld returned 1 exit status

I'm not sure if there is anything else that I have to do after installing the lib for the library to be accessible in my source code.

Update

I ran it with the -lbgp flag and when running it, i get the following error:

g++ -lbgp ambgp.cpp -o ambgp

Error:

./ambgp: error while loading shared libraries: libbgp.so.0: cannot open shared object file: No such file or directory

My Lib:

ls -l /usr/local/lib/

-rw-r--r-- 1 root root  10875880 Jan 18 16:56 libbgp.a
-rwxr-xr-x 1 root root       924 Jan 18 16:56 libbgp.la
lrwxrwxrwx 1 root root        15 Jan 18 16:56 libbgp.so -> libbgp.so.0.0.0
lrwxrwxrwx 1 root root        15 Jan 18 16:56 libbgp.so.0 -> libbgp.so.0.0.0
-rwxr-xr-x 1 root root   4291128 Jan 18 16:56 libbgp.so.0.0.0
drwxrwsr-x 3 root staff     4096 Dec 16 19:27 python3.7
echo $LD_LIBRARY_PATH
:/usr/local/lib
TheEagle
  • 5,808
  • 3
  • 11
  • 39
David
  • 105
  • 1
  • 10
  • Yes there is something more. You have to **link** the library with your source code. Since you seem to be using g++ you need to add `-lbgp` to your command line when you link your code. – john Jan 18 '21 at 17:07
  • In fact look at the examples section of the documentation where you can see that they do exactly what I said in the comment above. – john Jan 18 '21 at 17:09
  • I ran it as they stated and I'm getting the following: ``` g++ -lbgp ambgp.cpp -o ambgp ./ambgp: error while loading shared libraries: libbgp.so.0: cannot open shared object file: No such file or directory ``` – David Jan 18 '21 at 19:07
  • Have you searched for the shared library and ensured it exists/is installed? find / -name libbgp.so and see the results – Omid CompSCI Jan 18 '21 at 19:12
  • Yes, I found the shared library: /usr/local/lib/libbgp.so and /home//libbgp/src/.libs/libbgp.so – David Jan 18 '21 at 19:16

1 Answers1

0

Before running the executable, you have to tell it where to find the libgdp.so file, if it is not stored in a standard place such as /usr/lib or /lib. Following should help you:

$ export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/usr/local/lib"
$ ./ambgp

If you do not want to export the LD_LIBRARY_PATH each time you start the shell manually, add the line to your /home/<user>/.bashrc file. Additionally, i think the -lbgp flag should go after the source file in the compiler command (g++ ambgp.cpp -lbgp -o ambgp)

TL;DR :
ld states it cannot find libbgp.so.0, and you wrote in the comments that you found libbgp.so without a trailing .0. So, creating a symlink to the library could help too:

$ sudo ln -s /usr/local/lib/libbgp.so /usr/local/lib/libbgp.so.0

For linking, you need a library file without a trailing .0, but for loading, the library name must have a trailing .0.

The last thing to try is to directly specify the library location to the linker with -Wl,-rpath=/usr/local/lib (as you worked out already !)

TheEagle
  • 5,808
  • 3
  • 11
  • 39
  • I might show that `/usr/local/lib/libbgp.so.0` was not there, but I double check and it is present. I'm still getting the error. I also echo $LD_LIBRARY_PATH and that path was empty. I ran export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/usr/local/lib" and still getting the same error. – David Jan 18 '21 at 19:55
  • @David Please try `export LD_LIBRARY_PATH="/usr/local/lib"; ./ambgp` (i had an issue where setting `LD_LIBRARY_PATH` to `${LD_LIBRARY_PATH}:/some/path` when `LD_LIBRARY_PATH` was not previously defined or empty (`LD_LIBRARY_PATH` then would be `:/some/path` instead of `/some/path`) did not work) – TheEagle Jan 18 '21 at 20:43
  • @David if that works, i will add it to my answer. – TheEagle Jan 18 '21 at 20:47
  • @David if it doesn't work, i do not know what else you could try :/ – TheEagle Jan 18 '21 at 20:49
  • It does not work. Is there a way where the compiler is looking for shared library? – David Jan 18 '21 at 20:51
  • @David wait some moments, i will try it out myself. – TheEagle Jan 18 '21 at 20:52
  • I see /usr/local/lib/ under LIBRARY_PATH when I run g++ ambgp.cpp -lbgp -o ambgp -v – David Jan 18 '21 at 20:59
  • @David `LIBRARY_PATH` is used at compile time, `LD_LIBRARY_PATH` is used at runtime. – TheEagle Jan 18 '21 at 21:03
  • I compiled it with g++ ambgp.cpp -lbgp -o ambgp -Wl,-rpath=/usr/local/lib and it works. – David Jan 18 '21 at 21:06
  • hm, @David did you maybe run the program with sudo ? because when i run your program without sudo, it gives me `bind(): Permission denied`, when running it with sudo its giving me exactly the same error as you got. – TheEagle Jan 18 '21 at 21:07
  • Yes, it needs to be run as sudo do that the program is binding a low level port number. – David Jan 18 '21 at 21:08
  • @David ok, thats the reason then; sudo obviously doesn't import the environment variables, and as your solution hard-codes the library location into the binary, your program works without knowing `LD_LIBRARY_PATH`. Can you please accept my answer, i updated it accordingly. – TheEagle Jan 18 '21 at 21:12