Skip to content

Commit 7eedb61

Browse files
authored
doc: Shared+Static build for CMake (#517)
* doc: Shared+Static build for CMake Document the recommended way to build both Shared and Static libraries of HIDAPI using CMake. Closes: #424
1 parent fc3eed3 commit 7eedb61

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

BUILD.cmake.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,48 @@ Advanced:
233233
target_compile_definitions(hidapi_libusb PRIVATE NO_ICONV)
234234
endif()
235235
```
236+
237+
## Both Shared and Static build
238+
239+
If you're a former (or present) user of Autotools build scripts for HIDAPI, or you're a package manager maintainer and you're often working with those - you're likely asking how to build HIDAPI with CMake and get both Shared and Static libraries (as would be done by Autotools: `./configure --enable-static --enable-shared ...`).
240+
241+
CMake doesn't have such option of-the-box and it is decided not to introduce any manual CMake-level workarounds for HIDAPI on this matter.
242+
243+
If you want to mimic the Autotools behavior, it is possible by building/installing first the static version of the library and then shared version of the library. The installation folder (`CMAKE_INSTALL_PREFIX`) should point to the same directory for both variants, that way:
244+
- both static and shared library binaries will be available and usable;
245+
- a single header file(s) for both of them;
246+
- Autotools/pkg-config (`.pc`) files will be generated and usable _as if_ generated by Autotools natively and build configured with both `-enable-static --enable-shared` options;
247+
- CMake package scripts will be generated and fully usable, but _only the last build installed_, i.e. if the last was installed Shared version of the binary - CMake targets found by `find_package(hidapi)` would point to a Shared binaries.
248+
249+
There is a historical discussion, why such solution is simplest/preferable: https://github.com/libusb/hidapi/issues/424
250+
251+
#### TL;DR/Sample
252+
253+
```sh
254+
# First - configure/build
255+
256+
# Static libraries
257+
cmake -S <HIDAPI source dir> -B "<build dir>/static" -DCMAKE_INSTALL_PREFIX=<your installation prefix> -DBUILD_SHARED_LIBS=FALSE
258+
cmake --build "<build dir>/static"
259+
# Shared libraries
260+
cmake -S <HIDAPI source dir> -B "<build dir>/shared" -DCMAKE_INSTALL_PREFIX=<your installation prefix> -DBUILD_SHARED_LIBS=TRUE
261+
cmake --build "<build dir>/shared"
262+
263+
# (Optionally) change the installation destination.
264+
# NOTE1: this is supported by CMake only on UNIX platforms
265+
# See https://cmake.org/cmake/help/latest/envvar/DESTDIR.html
266+
# NOTE2: this is not the same as `CMAKE_INSTALL_PREFIX` set above
267+
# NOTE3: this is only required if you have a staging dir other than the final runtime dir,
268+
# e.g. during cross-compilation
269+
export DESTDIR="$STAGING_DIR"
270+
271+
#
272+
# Install the libraries
273+
# NOTE: order of installation matters - install Shared variant *the last*
274+
275+
# Static libraries
276+
cmake --install "<build dir>/static"
277+
# Shared libraries
278+
cmake --install "<build dir>/shared"
279+
280+
```

0 commit comments

Comments
 (0)