我正在尝试创建一个依赖于外部zlib的库。我尝试同时遵循this这样的问题和this one的问题,我的结构看起来像这样:
def room_version = "2.2.5"
def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
然后在我的CMakeLists.txt文件中,尝试这样构建库:
├── myLib (No CMakeLists.txt in this one)
├── zlib (git submodule)
├── build
├── CMakeLists.txt
现在,我尝试与add_library(ZLIB INTERFACE)
find_package(ZLIB)
if(NOT ZLIB_FOUND)
include(ExternalProject)
set(ZLIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/zlib)
set(ZLIB_BIN ${CMAKE_CURRENT_BINARY_DIR}/zlib_bin)
ExternalProject_Add(ZLIB_EXTERNAL
SOURCE_DIR ${ZLIB_DIR}
BINARY_DIR ${ZLIB_BIN}
)
add_dependencies(ZLIB ZLIB_EXTERNAL)
set(ZLIB_LIBRARIES ${ZLIB_BIN}/lib/${CMAKE_INSTALL_LIBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}zlib${CMAKE_STATIC_LIBRARY_SUFFIX})
set(ZLIB_INCLUDE_DIRS ${ZLIB_BIN}/include)
endif()
target_link_libraries(ZLIB INTERFACE ${ZLIB_LIBRARIES})
target_include_directories(ZLIB INTERFACE
$<BUILD_INTERFACE:${ZLIB_INCLUDE_DIRS}>
$<INSTALL_INTERFACE:include>
)
#....
add_library (${myLib_lib} STATIC ZLIB ${myLib_SOURCES})
add_dependencies(${myLib_lib} ZLIB)
target_link_libraries(${myLib_lib} ${ZLIB_LIBRARIES})
install(TARGETS ${myLib} ZLIB EXPORT ${PROJECT_NAME}
RUNTIME DESTINATION bin COMPONENT lib
LIBRARY DESTINATION lib${LIB_SUFFIX} COMPONENT lib
ARCHIVE DESTINATION lib${LIB_SUFFIX} COMPONENT lib
INCLUDES DESTINATION include
PUBLIC_HEADER DESTINATION include/myLib)
链接,我尝试将${ZLIB_LIBRARIES}
和CMAKE_ARGS
添加到INSTALL_COMMAND
。但是我认为我犯了一些基本的CMake。
我的CMake配置很好,但是在尝试构建时会引发以下错误:
ExternalProject_Add
。这告诉我无法找到我尝试定位的包含目录,并且在查看构建目录Cannot open include file: 'zlib.h': No such file or directory
时,它只是一个空文件夹,既没有库也没有包含文件夹。因此,没有一个失败的失败者,如何使其真正构建我要链接的外部项目?