From 07dec180622d6a3b94b915ae3716a926b2cd473b Mon Sep 17 00:00:00 2001 From: Jonas Witschel Date: Sun, 3 Nov 2019 14:06:03 +0100 Subject: [PATCH] Link against libzip/zlib when USE_INCLUDED_{LIBZIP,ZLIB}=OFF is used lib3MF.so is not explicitly linked against libzip or zlib even when USE_INCLUDED_LIBZIP=OFF or USE_INCLUDED_ZLIB=OFF are used. These dependencies are only specified in the "Libs" section of the pkg-config file. People trying to link the library directly without relying on the pkg-config file will face errors like /usr/bin/ld: lib3MF.so.1.8.1.0: undefined reference to `deflateInit2_' This can be immediately observed when trying to compile lib3mf with cmake -DUSE_INCLUDED_LIBZIP=OFF -DUSE_INCLUDED_ZLIB=OFF -DLIB3MF_TESTS=ON The solution is to link lib3mf against all the necessary libraries so that they can be pulled in automatically during linking. This allows to move them from "Libs" to "Libs.private" in the pkg-config file since they do not need to be specified explicitly any more. With this change, compilation with the test suite as shown above works. As another small improvement, use @LIBZIP_LDFLAGS@ and @ZLIB_LDFLAGS@ instead of statically specifying the required libraries. This guarantees that only the actually necessary libraries (depending on the value of USE_INCLUDED_...) are added as dependencies. --- CMakeLists.txt | 11 +++++++++++ lib3MF.pc.in | 3 ++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 784b61866..a29b99858 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -120,6 +120,17 @@ else() target_compile_options(${PROJECT_NAME}_s PUBLIC "$<$:/O2;/sdl;/WX;/Oi;/Gy;/FC;/wd4996>") endif() +if (NOT USE_INCLUDED_LIBZIP) + find_package(PkgConfig REQUIRED) + pkg_check_modules(LIBZIP REQUIRED libzip) + target_link_libraries(${PROJECT_NAME}_s ${LIBZIP_LIBRARIES}) +endif() +if (NOT USE_INCLUDED_ZLIB) + find_package(PkgConfig REQUIRED) + pkg_check_modules(ZLIB REQUIRED zlib) + target_link_libraries(${PROJECT_NAME}_s ${ZLIB_LIBRARIES}) +endif() + ######################################################### # Shared library diff --git a/lib3MF.pc.in b/lib3MF.pc.in index 9afb6ee88..0b022c5a3 100644 --- a/lib3MF.pc.in +++ b/lib3MF.pc.in @@ -8,5 +8,6 @@ Description: @PROJECT_DESCRIPTION@ Version: @PROJECT_VERSION@ Requires: -Libs: -L${libdir} -l3MF -lzip -lz +Libs: -L${libdir} -l3MF +Libs.private: @LIBZIP_LDFLAGS@ @ZLIB_LDFLAGS@ Cflags: -I${includedir}