我想libpng
从源代码构建一个库,这libz
又需要我也想从源代码构建。到目前为止,我有一个 CMakeLists.txt 文件,定义两个库的构建步骤的部分如下所示:
if(NOT EXISTS ${CMAKE_BINARY_DIR}/build-zlib/build/libz.a)
file(DOWNLOAD https://www.zlib.net/zlib-1.2.11.tar.gz zlib.tar.gz TLS_VERIFY ON)
file(ARCHIVE_EXTRACT INPUT zlib.tar.gz)
include(ExternalProject)
ExternalProject_Add(zlib
SOURCE_DIR ${CMAKE_BINARY_DIR}/zlib-1.2.11
BINARY_DIR ${CMAKE_BINARY_DIR}/build-zlib/build
INSTALL_COMMAND cmake -E echo "Skipping install"
CMAKE_ARGS -DCMAKE_POSITION_INDEPENDENT_CODE=ON
)
message(STATUS "zlib will be built during 'make'")
endif()
if(NOT EXISTS ${CMAKE_BINARY_DIR}/build-png/build/libpng.a)
include(ExternalProject)
ExternalProject_Add(png
GIT_REPOSITORY "https://github.com/glennrp/libpng.git"
GIT_TAG "v1.6.37"
BINARY_DIR ${CMAKE_BINARY_DIR}/build-png/build
INSTALL_COMMAND cmake -E echo "Skipping install"
CMAKE_ARGS -DCMAKE_POSITION_INDEPENDENT_CODE=ON
)
message(STATUS "libpng will be built during 'make'")
endif()
有两个问题:首先,CMake 不知道 libz 必须在 libpng 之前构建。其次,即使之前会构建 libz,CMake 也不知道 libz 将被放置在${CMAKE_BINARY_DIR}/build-zlib/build
. 我想第二个问题可以通过添加来解决list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR}/build-zlib/build)
(如果我错了,请纠正我)但是我该如何处理第一个问题?我自由地尝试添加add_dependencies(png zlib)
,但 libz 没有在 libpng 之前构建。因此,错误消息指出找不到 libz:
CMake Error at /snap/cmake/876/share/cmake-3.20/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR)