You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If building the application with cmake, the [Default RPATH settings](https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling#default-rpath-settings) include the full RPATH to all used libraries in the build tree. However, when installing, cmake will clear the RPATH of these targets so they are installed with an empty RPATH. This may result in a `Library not loaded` error after install.
215
+
216
+
Example:
217
+
```sh
218
+
# Build application `app` using the C++ driver from a non-standard install.
219
+
cmake \
220
+
-DCMAKE_PREFIX_PATH=$HOME/mongo-cxx-driver \
221
+
-DCMAKE_INSTALL_PREFIX=$HOME/app \
222
+
-DCMAKE_CXX_STANDARD=17 \
223
+
-Bcmake-build -S.
224
+
cmake --build cmake-build --target app.out
225
+
# Running app.out from build tree includes rpath to C++ driver.
226
+
./cmake-build ./cmake-build/app.out # Prints: "successfully connected with C++ driver"
227
+
228
+
cmake --build cmake-build --target install
229
+
# Running app.out from install tree does not include rpath to C++ driver.
230
+
$HOME/app/bin/app.out
231
+
# Prints "Library not loaded" error.
232
+
```
233
+
234
+
Consider setting `-DCMAKE_INSTALL_RPATH_USE_LINK_PATH=TRUE` so the rpath for the executable is kept in the install target.
235
+
```sh
236
+
# Build application `app` using the C++ driver from a non-standard install.
237
+
# Use CMAKE_INSTALL_RPATH_USE_LINK_PATH=TRUE to keep rpath entry on installed app.
238
+
cmake \
239
+
-DCMAKE_PREFIX_PATH=$HOME/mongo-cxx-driver \
240
+
-DCMAKE_INSTALL_PREFIX=$HOME/app \
241
+
-DCMAKE_INSTALL_RPATH_USE_LINK_PATH=TRUE \
242
+
-DCMAKE_CXX_STANDARD=17 \
243
+
-Bcmake-build -S.
244
+
245
+
cmake --build cmake-build --target install
246
+
$HOME/app/bin/app.out
247
+
# Prints "successfully connected with C++ driver"
248
+
```
249
+
250
+
See the cmake documentation for [RPATH handling](https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling) for more information.
251
+
252
+
### Fixing the "cannot open shared object file" error on Linux
253
+
254
+
Applications linking to a non-standard directory installation may encounter an error loading the C++ driver at runtime. Example:
255
+
256
+
```sh
257
+
# Tell pkg-config where to find C++ driver installation.
If building the application with cmake, the [Default RPATH settings](https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling#default-rpath-settings) include the full RPATH to all used libraries in the build tree. However, when installing, cmake will clear the RPATH of these targets so they are installed with an empty RPATH. This may result in a `Library not loaded` error after install.
285
+
286
+
Example:
287
+
```sh
288
+
# Build application `app` using the C++ driver from a non-standard install.
289
+
cmake \
290
+
-DCMAKE_PREFIX_PATH=$HOME/mongo-cxx-driver \
291
+
-DCMAKE_INSTALL_PREFIX=$HOME/app \
292
+
-DCMAKE_CXX_STANDARD=17 \
293
+
-Bcmake-build -S.
294
+
cmake --build cmake-build --target app.out
295
+
# Running app.out from build tree includes rpath to C++ driver.
296
+
./cmake-build ./cmake-build/app.out # Prints: "successfully connected with C++ driver"
297
+
298
+
cmake --build cmake-build --target install
299
+
# Running app.out from install tree does not include rpath to C++ driver.
300
+
$HOME/app/bin/app.out
301
+
# Prints "cannot open shared object file" error.
302
+
```
303
+
304
+
Consider setting `-DCMAKE_INSTALL_RPATH_USE_LINK_PATH=TRUE` so the rpath for the executable is kept in the install target.
305
+
```sh
306
+
# Build application `app` using the C++ driver from a non-standard install.
307
+
# Use CMAKE_INSTALL_RPATH_USE_LINK_PATH=TRUE to keep rpath entry on installed app.
308
+
cmake \
309
+
-DCMAKE_PREFIX_PATH=$HOME/mongo-cxx-driver \
310
+
-DCMAKE_INSTALL_PREFIX=$HOME/app \
311
+
-DCMAKE_INSTALL_RPATH_USE_LINK_PATH=TRUE \
312
+
-DCMAKE_CXX_STANDARD=17 \
313
+
-Bcmake-build -S.
314
+
315
+
cmake --build cmake-build --target install
316
+
$HOME/app/bin/app.out
317
+
# Prints "successfully connected with C++ driver"
318
+
```
319
+
320
+
See the cmake documentation for [RPATH handling](https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling) for more information.
0 commit comments