[CMake] Linking imported library to ExternalProject

Laurent Chauvin laurent.chauvin0 at gmail.com
Mon May 1 14:24:42 EDT 2017


Hello,

I'm trying to create a superbuild for my project, which require nifticlib
library. I am able to automatically download and compile it, as well as
import created libraries, however, I'm not sure how can I link it with
another external project?

Here is my CMake (the relevant part):

message(STATUS "Installing Nifti library.")

ExternalProject_Add(Nifticlib
  SOURCE_DIR "${PROJECT_BINARY_DIR}/deps/nifticlib"
  BINARY_DIR "${PROJECT_BINARY_DIR}/deps/nifticlib-build"
  INSTALL_DIR "${PROJECT_BINARY_DIR}/deps/nifticlib-install"
  GIT_REPOSITORY "${git_protocol}://gitlab.com/slckr/nifticlib.git"
  GIT_TAG "e26a94e947c210104223f9f49737392c742c1c5b"
  CMAKE_ARGS
  -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
  -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
  -DZLIB_INCLUDE_DIR:PATH=${ZLIB_INC_DIR}
  -DZLIB_LIBRARY_DEBUG:PATH=${ZLIB_LIB_DIR}/${ZLIB_LIB_NAME_DEBUG}
  -DZLIB_LIBRARY_RELEASE:PATH=${ZLIB_LIB_DIR}/${ZLIB_LIB_NAME_RELEASE}
  DEPENDS Zlib)

set(NIFTIIO_LIB_BASE_NAME "niftiio")
set(NIFTIIO_LIB_NAME
"${CMAKE_STATIC_LIBRARY_PREFIX}${NIFTIIO_LIB_BASE_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX}")

set(NIFTICDF_LIB_BASE_NAME "nifticdf")
set(NIFTICDF_LIB_NAME
"${CMAKE_STATIC_LIBRARY_PREFIX}${NIFTICDF_LIB_BASE_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX}")

set(ZNZ_LIB_BASE_NAME "znz")
set(ZNZ_LIB_NAME
"${CMAKE_STATIC_LIBRARY_PREFIX}${ZNZ_LIB_BASE_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX}")

ExternalProject_Get_Property(Nifticlib install_dir)
set(NIFTI_LIB_DIR ${install_dir}/lib)
set(NIFTI_INC_DIR ${install_dir}/include/nifti)
set(NIFTI_BIN_DIR ${install_dir}/bin)

add_library(niftiio STATIC IMPORTED)
set_target_properties(niftiio PROPERTIES IMPORTED_LOCATION
"${NIFTI_LIB_DIR}/${NIFTIIO_LIB_NAME}")

add_library(nifticdf STATIC IMPORTED)
set_target_properties(nifticdf PROPERTIES IMPORTED_LOCATION
"${NIFTI_LIB_DIR}/${NIFTICDF_LIB_NAME}")

add_library(znz STATIC IMPORTED)
set_target_properties(znz PROPERTIES IMPORTED_LOCATION
"${NIFTI_LIB_DIR}/${ZNZ_LIB_NAME}")


## My project
ExternalProject_Add(MyProject
  SOURCE_DIR "${PROJECT_BINARY_DIR}/deps/myproject"
  BINARY_DIR "${PROJECT_BINARY_DIR}/deps/myproject-build"
  INSTALL_DIR "${PROJECT_BINARY_DIR}/deps/myproject-install"
  GIT_REPOSITORY "${git_protocol}://gitlab.com/slckr/myproject.git"
  CMAKE_ARGS
      -DNIFTI_INC_DIR:PATH=${NIFTI_INC_DIR}
-DNIFTI_LIB_DIR:PATH=${NIFTI_LIB_DIR}
  DEPENDS Nifticlib)



However, I imported my nifticlib as niftiio,nifticdf, and znz. How can I
pass them to be linked with my ExternalProject MyProject?
Given that I pass the NIFTI_LIBRARY_DIR, do I just need to do a
target_link_libraries(MyProject niftiio nifticdf znz) in my CMakeLists.txt
of MyProject? Is it gonna find the proper library (as the name of the
library is platform dependent)?

Thank you.
-Laurent
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20170501/19e2ce90/attachment.html>


More information about the CMake mailing list