Skip to content

Commit 57e3761

Browse files
CXX-2579 use standard directories in main install instructions (#944)
* fix link rendering on configuration page * include `/lib` in CMAKE_INSTALL_RPATH instruction And explain rationale. This can help a consumer locate libbsoncxx.so if the consumer only links to full path of libmongocxx.so. * replace `/opt/` with `$HOME` To prevent requiring users to have additional permissions. Installing to `/opt` on macOS requires root permissions. * add "Installing to non-standard directories" section to advanced.md * add instructions for specifying a custom C driver install * use standard install directories in main installation instructions * add instructions for fixes to locate shared libraries * use <install-dir>, not /usr/local, for uninstall example Co-authored-by: Ezra Chung <[email protected]> * exclude `CMAKE_PREFIX_PATH` or `CMAKE_INSTALL_PREFIX` from unrelated examples * combine `For Linux and macOS` and `For Windows` sections The instructions are no longer platform specific. --------- Co-authored-by: Ezra Chung <[email protected]>
1 parent 04092c1 commit 57e3761

File tree

4 files changed

+202
-117
lines changed

4 files changed

+202
-117
lines changed

docs/content/mongocxx-v3/configuration.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ title = "Configuring the mongocxx driver"
99
In the mongocxx driver, most configuration is done via the [connection
1010
URI](https://docs.mongodb.com/manual/reference/connection-string/). Some
1111
additional connection options are possible via the
12-
[mongocxx::options::client] ({{< api3ref classmongocxx_1_1options_1_1client
12+
[mongocxx::options::client]({{< api3ref classmongocxx_1_1options_1_1client
1313
>}}) class.
1414
1515
## Configuring TLS/SSL
@@ -21,7 +21,7 @@ To enable TLS (SSL), set `tls=true` in the URI:
2121
By default, mongocxx will verify server certificates against the local
2222
system CA list. You can override that either by specifying different settings in
2323
the connection string, or by creating a
24-
[mongocxx::options::tls] ({{< api3ref classmongocxx_1_1options_1_1tls >}})
24+
[mongocxx::options::tls]({{< api3ref classmongocxx_1_1options_1_1tls >}})
2525
object and passing it to `tls_opts` on mongocxx::options::client.
2626

2727
For example, to use a custom CA or to disable certificate validation,
@@ -109,7 +109,7 @@ See the MongoDB server
109109
for more information about determining the subject name from the
110110
certificate.
111111

112-
The PEM file can also be specified using the [mongocxx::options::tls] ({{< api3ref classmongocxx_1_1options_1_1tls >}}) class, see the first "Configuring TLS/SSL" example above.
112+
The PEM file can also be specified using the [mongocxx::options::tls]({{< api3ref classmongocxx_1_1options_1_1tls >}}) class, see the first "Configuring TLS/SSL" example above.
113113

114114
### Kerberos (GSSAPI)
115115

docs/content/mongocxx-v3/installation/advanced.md

Lines changed: 195 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,26 @@ dependencies as static libraries rather than the typical shared libraries. Thes
2626
produce library artifacts that will behave differently. Ensure you have a complete understanding
2727
of the implications of the various linking approaches before utilizing these options.
2828

29-
### For Linux and macOS
30-
31-
#### Configuring with `mongocxx` 3.2.x or newer
29+
### Configuring with `mongocxx` 3.2.x or newer
3230

3331
Users have the option to build `mongocxx` as a static library. **This is not recommended for novice
3432
users.** A user can enable this behavior with the `-DBUILD_SHARED_LIBS` option:
3533

3634
```sh
3735
cmake .. \
3836
-DCMAKE_BUILD_TYPE=Release \
39-
-DBUILD_SHARED_LIBS=OFF \
40-
-DCMAKE_PREFIX_PATH=/opt/mongo-c-driver \
41-
-DCMAKE_INSTALL_PREFIX=/opt/mongo-cxx-driver
37+
-DBUILD_SHARED_LIBS=OFF
4238
```
4339

44-
#### Configuring with `mongocxx` 3.5.0 or newer
40+
### Configuring with `mongocxx` 3.5.0 or newer
4541

4642
Users have the option to build `mongocxx` as both static and shared libraries. A user can enable
4743
this behavior with the `-DBUILD_SHARED_AND_STATIC_LIBS` option:
4844

4945
```sh
5046
cmake .. \
5147
-DCMAKE_BUILD_TYPE=Release \
52-
-DBUILD_SHARED_AND_STATIC_LIBS=ON \
53-
-DCMAKE_PREFIX_PATH=/opt/mongo-c-driver \
54-
-DCMAKE_INSTALL_PREFIX=/opt/mongo-cxx-driver
48+
-DBUILD_SHARED_AND_STATIC_LIBS=ON
5549
```
5650

5751
Users have the option to build `mongocxx` as a shared library that has statically linked
@@ -61,57 +55,220 @@ Users have the option to build `mongocxx` as a shared library that has staticall
6155
```sh
6256
cmake .. \
6357
-DCMAKE_BUILD_TYPE=Release \
64-
-DBUILD_SHARED_LIBS_WITH_STATIC_MONGOC=ON \
65-
-DCMAKE_PREFIX_PATH=/opt/mongo-c-driver \
66-
-DCMAKE_INSTALL_PREFIX=/opt/mongo-cxx-driver
58+
-DBUILD_SHARED_LIBS_WITH_STATIC_MONGOC=ON
6759
```
6860

69-
### For Windows
61+
## Disabling tests
7062

71-
#### Configuring with `mongocxx` 3.2.x or newer
63+
Pass `-DENABLE_TESTS=OFF` as a cmake option to disable configuration of test targets.
7264

73-
Users have the option to build `mongocxx` as a static library. **This is not recommended for novice
74-
users.** A user can enable this behavior with the `-DBUILD_SHARED_LIBS` option:
65+
```sh
66+
cmake .. -DENABLE_TESTS=OFF
67+
cmake --build .. --target help
68+
# No test targets are configured.
69+
```
70+
71+
## Installing to non-standard directories
72+
73+
To install the C++ driver to a non-standard directory, specify `CMAKE_INSTALL_PREFIX` to the desired
74+
install path:
7575

7676
```sh
7777
cmake .. \
7878
-DCMAKE_BUILD_TYPE=Release \
79-
-DBUILD_SHARED_LIBS=OFF \
80-
-DCMAKE_PREFIX_PATH=C:\mongo-c-driver \
81-
-DCMAKE_INSTALL_PREFIX=C:\mongo-cxx-driver
79+
-DCMAKE_INSTALL_PREFIX=$HOME/mongo-cxx-driver
8280
```
8381

84-
#### Configuring with `mongocxx` 3.5.0 or newer
82+
Consider also specifying the `-DCMAKE_INSTALL_RPATH=` option to the `lib` directory of the install.
83+
This may enable libmongocxx.so to locate libbsoncxx.so:
8584

86-
Users have the option to build `mongocxx` as both static and shared libraries. A user can enable
87-
this behavior with the `-DBUILD_SHARED_AND_STATIC_LIBS` option:
85+
```sh
86+
cmake .. \
87+
-DCMAKE_BUILD_TYPE=Release \
88+
-DCMAKE_INSTALL_PREFIX=$HOME/mongo-cxx-driver \
89+
-DCMAKE_INSTALL_RPATH=$HOME/mongo-cxx-driver/lib
90+
```
91+
92+
If the C driver is installed to a non-standard directory, specify `CMAKE_PREFIX_PATH` to the install
93+
path of the C driver:
8894

8995
```sh
9096
cmake .. \
9197
-DCMAKE_BUILD_TYPE=Release \
92-
-DBUILD_SHARED_AND_STATIC_LIBS=ON \
93-
-DCMAKE_PREFIX_PATH=C:\mongo-c-driver \
94-
-DCMAKE_INSTALL_PREFIX=C:\mongo-cxx-driver
98+
-DCMAKE_PREFIX_PATH=$HOME/mongo-c-driver \
99+
-DCMAKE_INSTALL_PREFIX=$HOME/mongo-cxx-driver
95100
```
96101

97-
Users have the option to build `mongocxx` as a shared library that has statically linked
98-
`libmongoc`. **This is not recommended for novice users.** A user can enable this behavior with the
99-
`-DBUILD_SHARED_LIBS_WITH_STATIC_MONGOC` option:
102+
> *Note* If you need multiple paths in a CMake PATH variable, separate them with a semicolon like
103+
> this:
104+
> `-DCMAKE_PREFIX_PATH="/your/cdriver/prefix;/some/other/path"`
105+
106+
### Configuring with `mongocxx` 3.1.x or 3.0.x
107+
108+
Instead of the `-DCMAKE_PREFIX_PATH` option, users must specify the `libmongoc` installation
109+
directory by using the `-DLIBMONGOC_DIR` and `-DLIBBSON_DIR` options:
100110

101111
```sh
102112
cmake .. \
103113
-DCMAKE_BUILD_TYPE=Release \
104-
-DBUILD_SHARED_LIBS_WITH_STATIC_MONGOC=ON \
105-
-DCMAKE_PREFIX_PATH=C:\mongo-c-driver \
106-
-DCMAKE_INSTALL_PREFIX=C:\mongo-cxx-driver
114+
-DLIBMONGOC_DIR=$HOME/mongo-c-driver \
115+
-DLIBBSON_DIR=$HOME/mongo-c-driver \
116+
-DCMAKE_INSTALL_PREFIX=$HOME/mongo-cxx-driver
107117
```
108118

109-
## Disabling tests
119+
### Fixing the "Library not loaded" error on macOS
110120

111-
Pass `-DENABLE_TESTS=OFF` as a cmake option to disable configuration of test targets.
121+
Applications linking to a non-standard directory installation may encounter an error loading the C++ driver at runtime. Example:
112122

113123
```sh
114-
cmake .. -DENABLE_TESTS=OFF
115-
cmake --build .. --target help
116-
# No test targets are configured.
117-
```
124+
# Tell pkg-config where to find C++ driver installation.
125+
export PKG_CONFIG_PATH=$HOME/mongo-cxx-driver/lib/pkgconfig
126+
clang++ app.cpp -std=c++11 $(pkg-config --cflags --libs libmongocxx) -o ./app.out
127+
./app.out
128+
# Prints the following error:
129+
# dyld[3217]: Library not loaded: '@rpath/libmongocxx._noabi.dylib'
130+
# Referenced from: '/Users/kevin.albertson/code/app.out'
131+
# Reason: tried: '/usr/local/lib/libmongocxx._noabi.dylib' (no such file), '/usr/lib/libmongocxx._noabi.dylib' (no such file)
132+
# zsh: abort ./app.out
133+
```
134+
135+
The default `install name` of the C++ driver on macOS includes `@rpath`:
136+
```sh
137+
otool -D $HOME/mongo-cxx-driver/lib/libmongocxx.dylib
138+
# Prints:
139+
# /Users/kevin.albertson/mongo-cxx-driver/lib/libmongocxx.dylib:
140+
# @rpath/libmongocxx._noabi.dylib
141+
```
142+
143+
Including `@rpath` in the install name allows linking applications to control the list of search paths for the library.
144+
145+
`app.out` includes the load command for `@rpath/libmongocxx._noabi.dylib`. `app.out` does not have entries to substitute for `@rpath`.
146+
147+
There are several ways to consider solving this on macOS:
148+
149+
Pass `DYLD_FALLBACK_LIBRARY_PATH` to the directory containing the C++ driver libraries:
150+
151+
```sh
152+
DYLD_FALLBACK_LIBRARY_PATH=$HOME/mongo-cxx-driver/lib ./app.out
153+
# Prints "successfully connected with C++ driver"
154+
```
155+
156+
Alternatively, the linker option `-Wl,-rpath` can be passed to add entries to substitute for `@rpath`:
157+
```sh
158+
# Tell pkg-config where to find C++ driver installation.
159+
export PKG_CONFIG_PATH=$HOME/mongo-cxx-driver/lib/pkgconfig
160+
# Pass the linker option -rpath to set an rpath in the final executable.
161+
clang++ app.cpp -std=c++11 -Wl,-rpath,$HOME/mongo-cxx-driver/lib $(pkg-config --cflags --libs libmongocxx) -o ./app.out
162+
./app.out
163+
# Prints "successfully connected with C++ driver"
164+
```
165+
166+
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.
167+
168+
Example:
169+
```sh
170+
# Build application `app` using the C++ driver from a non-standard install.
171+
cmake \
172+
-DCMAKE_PREFIX_PATH=$HOME/mongo-cxx-driver \
173+
-DCMAKE_INSTALL_PREFIX=$HOME/app \
174+
-DCMAKE_CXX_STANDARD=11 \
175+
-Bcmake-build -S.
176+
cmake --build cmake-build --target app.out
177+
# Running app.out from build tree includes rpath to C++ driver.
178+
./cmake-build ./cmake-build/app.out
179+
# Prints: "successfully connected with C++ driver"
180+
181+
cmake --build cmake-build --target install
182+
# Running app.out from install tree does not include rpath to C++ driver.
183+
$HOME/app/bin/app.out
184+
# Prints "Library not loaded" error.
185+
```
186+
187+
Consider setting `-DCMAKE_INSTALL_RPATH_USE_LINK_PATH=TRUE` so the rpath for the executable is kept in the install target.
188+
```sh
189+
# Build application `app` using the C++ driver from a non-standard install.
190+
# Use CMAKE_INSTALL_RPATH_USE_LINK_PATH=TRUE to keep rpath entry on installed app.
191+
cmake \
192+
-DCMAKE_PREFIX_PATH=$HOME/mongo-cxx-driver \
193+
-DCMAKE_INSTALL_PREFIX=$HOME/app \
194+
-DCMAKE_INSTALL_RPATH_USE_LINK_PATH=TRUE \
195+
-DCMAKE_CXX_STANDARD=11 \
196+
-Bcmake-build -S.
197+
198+
cmake --build cmake-build --target install
199+
$HOME/app/bin/app.out
200+
# Prints "successfully connected with C++ driver"
201+
```
202+
203+
See the cmake documentation for [RPATH handling](https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling) for more information.
204+
205+
### Fixing the "cannot open shared object file" error on Linux
206+
207+
Applications linking to a non-standard directory installation may encounter an error loading the C++ driver at runtime. Example:
208+
209+
```sh
210+
# Tell pkg-config where to find C++ driver installation.
211+
export PKG_CONFIG_PATH=$HOME/mongo-cxx-driver/lib/pkgconfig
212+
g++ -std=c++11 app.cpp $(pkg-config --cflags --libs libmongocxx) -o ./app.out
213+
./app.out
214+
# Prints the following error:
215+
# ./app.out: error while loading shared libraries: libmongocxx.so._noabi: cannot open shared object file: No such file or directory
216+
```
217+
218+
There are several ways to consider solving this on Linux:
219+
220+
Pass `LD_LIBRARY_PATH` to the directory containing the C++ driver libraries:
221+
222+
```sh
223+
LD_LIBRARY_PATH=$HOME/mongo-cxx-driver/lib ./app.out
224+
# Prints "successfully connected with C++ driver"
225+
```
226+
227+
Alternatively, the linker option `-Wl,-rpath` can be passed to add `rpath` entries:
228+
```sh
229+
# Tell pkg-config where to find C++ driver installation.
230+
export PKG_CONFIG_PATH=$HOME/mongo-cxx-driver/lib/pkgconfig
231+
# Pass the linker option -rpath to set an rpath in the final executable.
232+
g++ app.cpp -std=c++11 -Wl,-rpath,$HOME/mongo-cxx-driver/lib $(pkg-config --cflags --libs libmongocxx) -o ./app.out
233+
./app.out
234+
# Prints "successfully connected with C++ driver"
235+
```
236+
237+
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.
238+
239+
Example:
240+
```sh
241+
# Build application `app` using the C++ driver from a non-standard install.
242+
cmake \
243+
-DCMAKE_PREFIX_PATH=$HOME/mongo-cxx-driver \
244+
-DCMAKE_INSTALL_PREFIX=$HOME/app \
245+
-DCMAKE_CXX_STANDARD=11 \
246+
-Bcmake-build -S.
247+
cmake --build cmake-build --target app.out
248+
# Running app.out from build tree includes rpath to C++ driver.
249+
./cmake-build ./cmake-build/app.out
250+
# Prints: "successfully connected with C++ driver"
251+
252+
cmake --build cmake-build --target install
253+
# Running app.out from install tree does not include rpath to C++ driver.
254+
$HOME/app/bin/app.out
255+
# Prints "cannot open shared object file" error.
256+
```
257+
258+
Consider setting `-DCMAKE_INSTALL_RPATH_USE_LINK_PATH=TRUE` so the rpath for the executable is kept in the install target.
259+
```sh
260+
# Build application `app` using the C++ driver from a non-standard install.
261+
# Use CMAKE_INSTALL_RPATH_USE_LINK_PATH=TRUE to keep rpath entry on installed app.
262+
cmake \
263+
-DCMAKE_PREFIX_PATH=$HOME/mongo-cxx-driver \
264+
-DCMAKE_INSTALL_PREFIX=$HOME/app \
265+
-DCMAKE_INSTALL_RPATH_USE_LINK_PATH=TRUE \
266+
-DCMAKE_CXX_STANDARD=11 \
267+
-Bcmake-build -S.
268+
269+
cmake --build cmake-build --target install
270+
$HOME/app/bin/app.out
271+
# Prints "successfully connected with C++ driver"
272+
```
273+
274+
See the cmake documentation for [RPATH handling](https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling) for more information.

docs/content/mongocxx-v3/installation/linux.md

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -101,54 +101,13 @@ cmake .. \
101101
-DCMAKE_INSTALL_PREFIX=/usr/local
102102
```
103103

104-
If installing to a non-standard directory (i.e., one which the dynamic loader does not search),
105-
consider specifying the `-DCMAKE_INSTALL_RPATH=` option:
106-
107-
```
108-
cmake .. \
109-
-DCMAKE_BUILD_TYPE=Release \
110-
-DCMAKE_INSTALL_PREFIX=/opt/mongo \
111-
-DCMAKE_INSTALL_RPATH=/opt/mongo
112-
```
113-
114-
In the Unix examples that follow, `mongocxx` is customized in these ways:
115-
116-
* `libmongoc` is found in `/opt/mongo-c-driver`.
117-
* `mongocxx` is to be installed into `/opt/mongo-cxx-driver`.
118-
119-
With those two distinct (arbitrary) install locations, a user would run this `cmake` command:
120-
```sh
121-
cmake .. \
122-
-DCMAKE_BUILD_TYPE=Release \
123-
-DCMAKE_PREFIX_PATH=/opt/mongo-c-driver \
124-
-DCMAKE_INSTALL_PREFIX=/opt/mongo-cxx-driver
125-
```
126-
127-
> *Note* If you need multiple paths in a CMake PATH variable, separate them with a semicolon like
128-
> this:
129-
> `-DCMAKE_PREFIX_PATH="/your/cdriver/prefix;/some/other/path"`
130-
131104
These options can be freely mixed with a C++17 polyfill option. For instance, this is how a user
132105
would run the command above with the Boost polyfill option:
133106
```sh
134107
cmake .. \
135108
-DCMAKE_BUILD_TYPE=Release \
136109
-DBSONCXX_POLY_USE_BOOST=1 \
137-
-DCMAKE_PREFIX_PATH=/opt/mongo-c-driver \
138-
-DCMAKE_INSTALL_PREFIX=/opt/mongo-cxx-driver
139-
```
140-
141-
#### Configuring with `mongocxx` 3.1.x or 3.0.x
142-
143-
Instead of the `-DCMAKE_PREFIX_PATH` option, users must specify the `libmongoc` installation
144-
directory by using the `-DLIBMONGOC_DIR` and `-DLIBBSON_DIR` options:
145-
146-
```sh
147-
cmake .. \
148-
-DCMAKE_BUILD_TYPE=Release \
149-
-DLIBMONGOC_DIR=/opt/mongo-c-driver \
150-
-DLIBBSON_DIR=/opt/mongo-c-driver \
151-
-DCMAKE_INSTALL_PREFIX=/opt/mongo-cxx-driver
110+
-DCMAKE_INSTALL_PREFIX=/usr/local
152111
```
153112

154113
### Step 5: Build and install the driver
@@ -181,6 +140,6 @@ sudo cmake --build . --target uninstall
181140
Second, the uninstall script can be called:
182141

183142
```sh
184-
sudo /opt/mongo-cxx-driver/share/mongo-cxx-driver/uninstall.sh
143+
sudo <install-dir>/share/mongo-cxx-driver/uninstall.sh
185144
```
186145

0 commit comments

Comments
 (0)