0

I have a Makefile setup to build unit tests for my application:

test: app_test.o sdk.o
    g++ -L/usr/lib/x86_64-linux-gnu/ --coverage -o test app_test.o sdk.o ./sdk/libNSomeLib.so.2.6 -lboost_unit_test_framework

I have a folder in the root of my project directory called sdk, which libNSomeLib.so.2.6 is in.

When running locally, I can run make test and then ./test and have the tests run.

However, I'm also running the following in Azure DevOps, as part of a build pipeline:

  1. make test
  2. ./test

and receive:

./test: error while loading shared libraries: libNSomeLib.so.2: cannot open shared object file: No such file or directory

I had initially tried "installing" this library that I need onto each system I need to run it on, and updating ldconfig, but it's not feasible so I decided to statically link the library instead, and include the .so file in the repository itself.

So why is this happening if I'm statically linking, and how would I fix it? It seems to build fine, it's just the running part that isn't working.

Some Guy
  • 351
  • 1
  • 4
  • 20
  • 2
    (1) You cannot statically link with a shared library. (2) No need to touch ldconfig. Read about `-rpath` e.g. [here](https://stackoverflow.com/questions/8482152/whats-the-difference-between-rpath-and-l). – n. m. could be an AI Nov 11 '22 at 19:00
  • @n.m. So pretty much it's needed that I "install" the lib into a known path, add the directory using `-L`, and then link it using `-l`? – Some Guy Nov 11 '22 at 19:03
  • If you build and run on the same machine, then the library is already on that machine at a known path (otherwise you would not be able to build). Use that path as the `-rpath` argument. If you build on one machine and run on a different machine, you need to transport the library to the target machine somehow, and use `-rpath` relevant to the target machine. (You can use a path relative to the executable; read about `$ORIGIN`). – n. m. could be an AI Nov 11 '22 at 19:08
  • @n.m. Is there no way to embed that library into the final executable I'm trying to build? – Some Guy Nov 11 '22 at 19:11
  • There might be, but you don't want it. Just copy it together with the executable. – n. m. could be an AI Nov 11 '22 at 19:17

1 Answers1

0

You can simply copy "test" and "sdk/libNSomeLib.so.2.6" to any machine and can excute it without ldconfig. if you dislike "sdk" dir. You should build it with

g++ app_test.o.sdk.o -Lsdk -lNSomeLib -o test -Wl,-rpath=.

after that, you place both "test" and "libNSomeLib.so" in same director and you can execute "test" because of searching .so by rpath=. option embedded in "test" executable

shy45
  • 457
  • 1
  • 3