Skip to content
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

Cmake: build using FetchContent #600

Open
mariuszmaximus opened this issue Sep 20, 2024 · 1 comment
Open

Cmake: build using FetchContent #600

mariuszmaximus opened this issue Sep 20, 2024 · 1 comment

Comments

@mariuszmaximus
Copy link

I try build libpng using Cmake::FetchContent

cmake_minimum_required(VERSION 3.19)
project(png-usage)

# more logging from FetchContent
set(FETCHCONTENT_QUIET OFF)

include(FetchContent)

FetchContent_Declare(
  zlib_repo
  GIT_REPOSITORY "https://github.com/madler/zlib"
)
FetchContent_MakeAvailable(zlib_repo)

# app_zlib
add_executable(app_zlib app_z.cpp)
target_link_libraries(app_zlib zlib)

FetchContent_Declare(
  libpng
  GIT_REPOSITORY "https://github.com/glennrp/libpng.git"
)
set(PNG_TESTS OFF)
FetchContent_MakeAvailable(libpng)


# app_png.
add_executable(app_png app_png.cpp)
target_link_libraries(app_png png_static)

But i have error: Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR)

Is there any way to fix this?
Where is the problem? In libpng or zlib?

I tested Linux (gcc), Windows (msvc, msys2-clang)
It doesn't work anywhere

full repo https://github.com/mariuszmaximus/libpng_usage.git

@Epixu
Copy link

Epixu commented Oct 7, 2024

ZLIB and PNG have horrid modern CMake support. It is generally done by setting ZLIB_ROOT nowadays, however it doesn't play along with FetchContent at all. You have to relay your fetched ZLIB towards your fetched PNG.

Here's how I managed to do it

^ Works by creating a local ALIAS target called zlibstatic, and then works around find_pakage(ZLIB) in png's cmake config by setting ZLIB_LIBRARY and ZLIB_INCLUDE_DIR manually. ZLIB_ROOT is still required for some reason. I've tried many other things, but this seems to be the only thing that works. If anyone gets a better idea, please share. I've tested building on windows/linux, with g++/clang/cl/clang-cl.

PS: fetch_external_module is just a wrapper for FetchContent_Declare and FetchContent_MakeAvailable - you can find it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants