我的程序使用 CImg 并且需要 tiff 和 cmath。
...
# Define the Tiff External Project
set(ep TIFF)
set(cmake_cache_args
-DBUILD_SHARED_LIBS:BOOL=OFF
-Dtiff-docs:BOOL=OFF
-Dtiff-test:BOOL=OFF
-Dtiff-tools:BOOL=OFF
-Djpeg:BOOL=OFF
-Doldjpeg:BOOL=OFF
-Djpeg12:BOOL=OFF
-Dlzma:BOOL=OFF
-Dzstd:BOOL=OFF
-Dwebp:BOOL=OFF
-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_BINARY_DIR}/ep/${ep}
)
ExternalProject_Add(${ep}
PREFIX ${CMAKE_BINARY_DIR}/ep/${ep}
GIT_REPOSITORY https://gitlab.com/faxguy/libtiff-tools.git
GIT_TAG master
CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release
CMAKE_CACHE_ARGS ${cmake_cache_args}
BUILD_COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR>
INSTALL_COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target install
)
...
# Link and compile GcoPS project
find_package (TIFF REQUIRED COMPONENTS tiff)
set(TARGET_NAME GcoPS)
add_executable(${TARGET_NAME})
target_sources(${TARGET_NAME} PRIVATE main.cpp)
target_link_libraries (${TARGET_NAME} PUBLIC TIFF::tiff)
当我静态编译所有内容时(在 mac、arm64 上),出现以下错误:
-- Configuring done (0.1s)
CMake Error at /path/to/GcoPS-build-static/ep/TIFF/lib/CMake/tiff/TiffTargets.cmake:61 (set_target_properties):
The link interface of target "TIFF::tiff" contains:
CMath::CMath
but the target was not found. Possible reasons include:
* There is a typo in the target name.
* A find_package call is missing for an IMPORTED target.
* An ALIAS target is missing.
Call Stack (most recent call first):
/path/to/GcoPS-build-static/ep/TIFF/lib/CMake/tiff/TIFFConfig.cmake:23 (include)
CMakeLists.txt:37 (find_package)
问题是 tiff 库生成一个
FindCMath.cmake
文件 (path/to/build/TIFF/src/TIFF/cmake/FindCMath.cmake
),它在我的程序中隐藏了 cmath。
解决方案是在构建我的程序之前找到这个模块:
...
# ADD PATH TO THE FOLDER CONTAINING path/to/build/TIFF/src/TIFF/cmake/FindCMath.cmake
list(APPEND CMAKE_MODULE_PATH "${CMAKE_BINARY_DIR}/../ep/TIFF/src/TIFF/cmake/")
find_package (TIFF REQUIRED COMPONENTS tiff)
# ADD THE TIFF CMATH
find_package (CMath)
set(TARGET_NAME GcoPS)
add_executable(${TARGET_NAME})
target_sources(${TARGET_NAME} PRIVATE main.cpp vesicleDisplaying3D.cpp)
target_link_libraries (${TARGET_NAME} PUBLIC TIFF::tiff)