IMPORTED libraries have unmovable paths on LINUX

Hi Everybody. I am working on a project that has to be compiled for Windows and Linux. Also I have 2 external libraries that are part of the project:

  1. boost program_options - this one I want to be included in the program so I made it STATIC IMPORTED
  2. a in house .so file - this one should be provided next to the program so I made it SHARED IMPORTED

For both of these I specified a IMPORTED_LOCATION.

Somehow my CMake set up works just fine on windows, I think its due to the priority order dll’s use in Windows. But in Linux it creates dependencies to the absolute paths of the .so files that were used in the build process and when I move the program to a different location / computer the .so files can not be found.

Any help would be really appreciated and thanks in advance!

This is normal behavior for a linux build. On Windows, the .dll’s probably reside in the same folder as your executable? Windows adds the current folder as a standard search path, while Linux does not, unless you specify it in the RPATH, which typically contains a hard-coded path to the shared object.

See RPATH handling in the CMake wiki, specifically the text about $ORIGIN.

Hi codeling, thanks for your response.

I managed to add $ORIGIN to the RPATH with: set(CMAKE_BUILD_RPATH $ORIGIN)

When I run the readelf -d myBinary command I can see that the RPATH is set to include $ORIGIN but I also see that boost program options library and my .so library are both listed as NEEDED dependencies.

When I include the boost program options .so libraries along side my program it is able to run, however I am surprised that this is required seeing as I made it a STATIC IMPORTED library. Is there something else I have to do to include boost program options within my program?

Additionally the readelf -d command showed me that my .so library is NEEDED and the full path to the file is listed as the dependency, when I place the .so library alongside my program it still does not work. This is the one I set to SHARED IMPORTED, is there anything else I have to do in CMake to get it to work?

Thanks for your help!

If I understand correctly - you have boost shared libraries, but you want to link them statically to your binary? I think that’s not possible, as far as I understand it, whether you link a library statically or dynamically is not determined by your build, but by the type of library you link to. So, under linux, you link to .so files dynamically and to .a files statically; that is, if you link against an .so file, you will also require the .so at run time; if you link against an .a file, all the required parts will be included in your own library/executable, and you will not need the .a file when running your application.

1 Like

Thank you!
That solved my problem with the boost library. I still have another .so library that I want to be linked dynamically but for some reason the exact location where the .so was during the build becomes a dependency at run time. And I have $ORIGIN in the RPATH.

For the record the solution was to use

include_directories() to point the project to the header files and

target_link_directories() to link the target with the .so file

That eliminated the dependency on a specific path to the .so file.