0

I have been developing a research code using CMake to generate the Makefiles for a c++ code on an Ubuntu machine. I link in several shared libraries which are rather involved to setup and build on a machine (one in particular has a dozen or so version specific dependencies which themselves are non trivial to build). Some are also custom builds of the library (bug fixes).

I am more familiar with the windows environment with the CLR, but, what I am hoping to achieve is building the binary and including all of the shared libraries along with it. Hopefully the linker on the other environment (redhat EL6) would then be able to use those shared objects at runtime.

Since the linker doesn't look in the applications path, I assume I would also need to bring the shared libraries into a user specific library path for it to find.

Is there a nice way using Cmake (perhaps Cpack), to build the binary and 'package' all of the shared objects with it for the other machine? Then I could (even if manually) install the shared libraries for my user only, and run the binary there.

I'm hoping the answer is not using static libraries, as that has given me a lot of trouble for these dependencies in the past.

I'm a linux noob, so if my issues is in lack of understanding a better approach I am all ears :)

ccook
  • 5,869
  • 6
  • 56
  • 81
  • 1
    Are these shared libraries that are part of your project, or nonstandard versions of shared libraries that might conflict with installed libraries? – Joshua Clayton Apr 30 '14 at 19:52
  • most are shared libraries of common packages, such as boost, however, one in particular is a custom build off of the dev branch for a project (fenics-project). Building that project from source is a bit of a nightmare in tracking down and building its dependencies (such as trilinos). Since the destination is a shared environment I'd like avoid that route. I'm thinking I might need to setup a VM with redhat EL6 and build it there, then just copy the shared libraries to my user path (manually). – ccook Apr 30 '14 at 19:57

1 Answers1

2

One approach is to set the "rpath" of the binary, which hardcodes some additional search paths. One can set it to $ORIGIN, which means that libraries in the same directory as the binary itself will be used first.

IF(UNIX)
  SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH}:\$ORIGIN/../bin:\$ORIGIN")
ENDIF()

This CMake code takes effect at install time (make install), so first you need to set up the INSTALL commands of your CMake setup. INSTALL both your binary and all extra shared objects. Finally, CPack internally runs an "install", so once make install works, you can use CPack to automatically build a TGZ, or even an RPM if you're willing to invest the time to get it set up.

Here's an old answer of mine which talks about a similar but not identical issue, linked for completeness: https://stackoverflow.com/a/22209962/1401351

Community
  • 1
  • 1
Peter
  • 14,559
  • 35
  • 55