Macintron
Macintron

Reputation: 107

Issue with fixup_bundle() removing RPATH in CMake Build

I’m encountering a problem with fixup_bundle() in my CMake-based build process. My executable, app, links to a shared library bibliA, which itself depends on another shared library, bibliPrivate.

When I call fixup_bundle(), both bibliA and bibliPrivate are copied to the package directory alongside the app executable. However, fixup_bundle() removes the RPATH, and as a result, the final package fails to run with the following error: error while loading shared libraries: libbibliA.so: cannot open shared object file: No such file or directory.

ldd app/libbibliA.so shows => not found in the _CPack_Packages directory, while the binaries in the build directory run.

In my setup, app, bibliA, and bibliPrivate are all in the same directory in the package.

Here is the build command I’m using:

cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -S .. && ninja -v && ninja -v package

What could be causing this issue, and how can I ensure that the RPATH are correctly resolved in the final package?

Here is the CMakeLists.txt which also creates the cpp-source files in the build directory:

cmake_minimum_required(VERSION 3.27)
project(Sandbox VERSION 0.1)

# Shared Library bibliA
file(WRITE ${CMAKE_BINARY_DIR}/bibliA.hpp
[[#include <string>
std::string bibliAVersion();
]])
file(WRITE ${CMAKE_BINARY_DIR}/bibliA.cpp
[[#include "bibliA.hpp"
#include "bibliPrivate.hpp"
std::string bibliAVersion() {
  return std::string("Hello from bibliA\n  ") + privateVersion();
}
]])
add_library(bibliA SHARED bibliA.cpp)
target_include_directories(bibliA PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(bibliA PRIVATE bibliPrivate)

# Shared Library bibliPrivate
file(WRITE ${CMAKE_BINARY_DIR}/bibliPrivate.hpp
[[const char* privateVersion();]])
#    "const char* privateVersion();\n")
file(WRITE ${CMAKE_BINARY_DIR}/bibliPrivate.cpp
[[const char* privateVersion() {
  return "bibliPrivate 1.0.0";
}
]])
add_library(bibliPrivate SHARED bibliPrivate.cpp)
target_include_directories(bibliPrivate PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

# executable for app
file(WRITE ${CMAKE_BINARY_DIR}/main.cpp
[[#include <iostream>
#include "bibliA.hpp"
int main() {
  std::cout << "App\n" << bibliAVersion() << std::endl;
  return 0;
}
]])
add_executable(app main.cpp)
target_link_libraries(app PUBLIC bibliA)


# installation
install(
  TARGETS app
  DESTINATION bin
)

set(_bundleDir \${CMAKE_INSTALL_PREFIX}/bin/app)
set(_bundle_libs)
set(_dirs ${CMAKE_CURRENT_BINARY_DIR})
install(CODE "
  include(BundleUtilities)
  set(GET_PREREQUISITES_VERBOSE ON)
  fixup_bundle(\"${_bundleDir}\" \"${_bundle_libs}\" \"${_dirs}\")
  verify_app(\"${_bundleDir}\")
    "
)

# Package
set(CPACK_GENERATOR TGZ)
include(CPack)

Upvotes: 0

Views: 25

Answers (0)

Related Questions