CMake: wrong zlib found - how to build zlib from src (with the main CMakeLists.txt project) and link it?

// short version

How can I get CMake to use the zlib I provided (which it should also build from the source) and not the finder found without breaking the search for other libs (OpenGL)?

ZLib should be used by the main project, as well as libPNG, which is also the source.

The primary target platform is Windows.

// longer version:

In my project, I need to set the link to libpng, zlib and OpenGL. C libpng depends on zlib. But zlib is also required for the main project.

I need to provide source code for all libraries except OpenGL, and assemble these libraries together with the main project in order to approve the link to the correct version and simplify the creation on Windows.

I found ways to do all this with custom libraries where there is no built-in crawler, but I cannot override the crawler correctly only for zlib. If I changed the search path for libs, then OpenGL will not be found.

However, I cannot get cmake to use my supplied zlib instead of rouge zlib.DLL, which the search engine finds somewhere on my system. (One of the turtle git)

I tried setting ZLIB_LIBRARY to a specific file path, but this only works on MinGW, and I also think that this is not a way to do this.

(And also I had to explicitly refer to png16_static, and not just libpng, for an unexplained reason.)

Any help on this is greatly appreciated. Maybe I take it wrong?


Target & Development Platform:

Windows7 Visual Studio 2010 and MinGW (both need to work) 

My (simplified example) CMakeLists.txt:

 cmake_minimum_required (VERSION 2.6) project (MyProject) find_package(OpenGL) add_executable(MyProject main.cpp) include_directories(${INCLUDE_DIRECTORIES} "${PROJECT_BINARY_DIR}") include_directories(${INCLUDE_DIRECTORIES} "external_libs/lpng162") include_directories(${INCLUDE_DIRECTORIES} "external_libs/zlib-1.2.8") include_directories(${INCLUDE_DIRECTORIES} "${PROJECT_BINARY_DIR}/external_libs/zlib-1.2.8") add_subdirectory("external_libs/zlib-1.2.8") link_directories(${LINK_DIRECTORIES} "${PROJECT_BINARY_DIR}/external_libs/zlib-1.2.8") # libpng will not build correctly if this not set set (ZLIB_ROOT "${PROJECT_SOURCE_DIR}/external_libs/zlib-1.2.8") # manually set this to prevent cmake from finding the tortiose-git zlib.dll first # DOES NOT WORK CORRECTLY, only with mingw32 set (ZLIB_LIBRARY "${PROJECT_BINARY_DIR}/external_libs/zlib-1.2.8/libzlib.dll") add_subdirectory("external_libs/lpng162") TARGET_LINK_LIBRARIES(MyProject png16_static zlib ${OPENGL_LIBRARY}) 

Project structure (simplified example):

 ./main.cpp ./CMakeLists.txt ./external_libs/zlib-1.2.8/ <- contains respective source ./external_libs/lpng162/ <- contains respective source 
+4
source share
1 answer

Third-party libraries most likely call FindZLIB.cmake to locate CMake. You already had the right idea by setting ZLIB_LIBRARY manually, but not quite understanding:

 add_subdirectory(<path_to_zlib_src_dir>) set(ZLIB_INCLUDE_DIR "<path_to_zlib_src_dir>" "${CMAKE_BINARY_DIR}/<path_to_zlib_build_dir>") set(ZLIB_LIBRARY zlib) add_subdirectory(<path_to_lpng_src_dir>) 
  • The include directory must contain both src and the build path, since zconf.h is created using CMake
  • The library name is just the name of the CMake target, not the full path to the resulting file.
  • On Windows, dlls do not automatically copy CMake. You might want to add additional code to make sure that the dll zlib and lpng are in the right place.
  • You can call find_package(zlib) yourself to make sure that it behaves as expected
  • In the rare case when a third-party library does not use find script, you will have to delve into this CMakeLists project to find out what is happening.
+2
source

Source: https://habr.com/ru/post/1491853/


All Articles