-
Notifications
You must be signed in to change notification settings - Fork 408
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Building both static and shared libraries at the same time #424
Comments
The way you're building it right now is the cleanest possible with CMake at the moment. The only implication of the overwriting CMake files is that when a CMake user would try to use HIDAPI with In your example, CMake's This may be somethign that could be improved on HIDAPI side, but I need to understad one thing: When a library is built and installed in the system, and there is both static and shared variants of the library + Normally, if the user needs to use the library, the common approach is to use pkg-config, e.g.: How exactly user gets to choose the static version of the library w/o manually hard-coding the path to it? |
BTW: I recommend installing static version first, and then shared, so by default your CMake users would use a shared version of the library - to be consistent with pkgconfig users in general. |
Hi,
In ¹ we can read :
However, if you want to link to a specific library statically (and others dynamically), you can use an option for the linker for that specific library :
See the following option in ² (but the documentation seems not very clear to me) :
Tested here and it seems to work fine. |
My analysis:
I'd say this is practically a "manually hard-coding the path" (library name).
CMake doesn't have any similar machanism(s), so I don't think it worth making anything HIDAPI-specific in this matter on CMake level. Basically, for now I'd say what is done at https://gitlab.com/nomadic-labs/hidapi-apk/-/blob/main/APKBUILD#L37 is fine (and can be used as a reference for others who need a similar thing). The only true alternative, is to do something that, for instance, libjpeg-turbo does (here and here), but that would complicate CMake scripts for HIDAPI a lot, has complicated corner-cases (e.g. the Framework libraries on macOS), and I'm not sure it is really required. |
Yes, but it is always possible to generate with cmake an additional *.pc file to link to the static archive.
And in cmake files : index e5d7d51..d073545 100644
--- a/libusb/CMakeLists.txt
+++ b/libusb/CMakeLists.txt
@@ -65,3 +65,4 @@ if(HIDAPI_INSTALL_TARGETS)
endif()
hidapi_configure_pc("${PROJECT_ROOT}/pc/hidapi-libusb.pc.in")
+hidapi_configure_pc("${PROJECT_ROOT}/pc/hidapi-libusb-static.pc.in" "hidapi-libusb")
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 504b205..c8b0d4b 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -51,6 +51,9 @@ function(hidapi_configure_pc PC_IN_FILE)
set(VERSION "${VERSION}${VERSION_SUFFIX}")
set(prefix "${CMAKE_INSTALL_PREFIX}")
set(exec_prefix "\${prefix}")
+ foreach(arg IN LISTS ARGN)
+ set(OUTPUT_NAME "${arg}")
+ endforeach()
if(IS_ABSOLUTE "${CMAKE_INSTALL_LIBDIR}")
set(libdir "${CMAKE_INSTALL_LIBDIR}")
else() Once all this stuff built and installed, the user may chose between static or dynamic linking by choosing a different *pc file :
I was just answering the question how a user can choose to link to the static library when static and dynamic are both installed. I do not know cmake more than that. |
Technically that is an entirely different library, which happens to have (almost) the same name. But thanks for suggestion.
I guess the complete answer I was looking for:
|
To be entirely complete on this subject, the additional pc file can be installed in a non-standard directory :
But, yes, it seems that there is no other way. |
Interesting there are similar discussions here -- looks like a common CMake question. My comment here:
|
For example, if I only build the default dynamic link version, then I will get wrong static link option using pkg-config.
|
So far I haven't seen a single pkg-config package, that would provide link options to link against static version of the library, when pass |
A common question of those who started using CMake after Autotools. Context/bias is important. |
I can see libusb-1.0 (autools) and libftdi-1.0 (CMake) try to differentiate between dynamic linking and static linking.
|
True, but pass those to the linker of you own library/application - you're still going to be linked against shared/.so version of libusb, right? |
@Youw |
avrdude-libhidapi is basically repackged hidapi (old version) adapted to MSVC static build. For MSVC, it makes sense to use static build. Is it possible to modify hidapi so that there is no need for avrdude to use a fork version of hidapi? I tend to see that avrdude has legitimate reasons to use avrdude-libwinusb (at least when libusb-1.0.23/1.0.24 are broken under Windows for libusb0.sys) and avrdude-libftdi (to use FTDI vendor driver) for MSVC build (Static linking) but no good reasons to use a fork version of hidapi for MSVC build. |
🤯 |
It seems to me that we are not going to fix this one. Maybe this can be closed. |
let me make a readme section as a summary of this one, and then we can close it |
Document the recommended way to build both Shared and Static libraries of HIDAPI using CMake. Closes: #424
* doc: Shared+Static build for CMake Document the recommended way to build both Shared and Static libraries of HIDAPI using CMake. Closes: #424
Thanks for the documentation and the help overall 🙏🏻 |
Nomadic Labs is using
libusb
andhidapi
for the Octez project at https://gitlab.com/tezos/tezos written in Ocaml.Is there a clean way to compile and install both static and shared libraries at the same time?
@pietro submitted an MR to Alpine
aports
to have both static and shared libraries for packagelibusb-dev
and is now available in Alpine3.15+
:https://gitlab.alpinelinux.org/alpine/aports/-/merge_requests/23613
We did the same for package
hidapi-dev
but the switch tocmake
removed the static libraries from Alpine3.15
starting with0.11.0
:https://gitlab.alpinelinux.org/alpine/aports/-/merge_requests/23615
https://gitlab.alpinelinux.org/alpine/aports/-/blob/master/community/hidapi/APKBUILD
At the moment we have no other solutions than building from source or use a custom Alpine package. We copied the current GitHub workflow for Ubuntu and run the configure/build/install twice:
https://gitlab.com/nomadic-labs/hidapi-apk/-/blob/main/APKBUILD#L21
I have no experience with
cmake
, my concern is that we overwrite the following files instlalling twice:Note: Alpine
aports
MR https://gitlab.alpinelinux.org/alpine/aports/-/merge_requests/35251The text was updated successfully, but these errors were encountered: