How to use FetchContent in CMakeList.txt to download/build SimpleITK and create example

I want to have an auto-configuration of SimpleITK and create the example project.
Here is an example CMakeList.txt:

cmake_minimum_required(VERSION 3.16.3)

project(exampleSITK)
set(BUILD_TESTING OFF)
set(BUILD_EXAMPLES OFF)

include(FetchContent) 

FetchContent_Declare(
  SimpleITK
  GIT_REPOSITORY https://github.com/SimpleITK/SimpleITK.git
  FIND_PACKAGE_ARGS NAMES SimpleITK
)

FetchContent_MakeAvailable(SimpleITK)

set(SimpleITK_DIR ${SimpleITK_BINARY_DIR} CACHE STRING "Make the SimpleITK_DIR available to other modules" FORCE)
find_package(SimpleITK REQUIRED CONFIG)
include(${SimpleITK_USE_FILE})

add_executable ( exampleSITK OpenImage.cpp )
target_link_libraries ( exampleSITK ${SimpleITK_LIBRARIES} )

It seems the SimpleITK is not build and it also shows an error of <could not find a package configuration file provided by ''itk" with any of the following names: ITKConfig.cmake itk-config.cmake>

Could anyone help advise?

To build SimpleITK from source, you need to point it to an ITK build (and all its other dependencies, if you turn on wrapping). This is no different from any other project using FetchContent approach. Going further in this direction, you can FetchContent more libraries (e.g. ITK) before SimpleITK, or full-blown superbuild.

The documentation for building SimpleITK is here:
https://simpleitk.readthedocs.io/en/master/building.html

It has itā€™s own Superbuild which builds dependencies such as ITK and SWIG.

I would recommend manually building SimpleITK before trying to create an ā€œauto-buildā€ SimpleITK.

1 Like

Thanks for the reply. I can successfully build and configure the SimpleITK project manually. However, we need ā€œautoā€ method to run the CMakeList.txt that other members can easily download/build/configure the project at individual machine.

I modified the CMakeList as following, but still have errors from terminal:

By not providing ā€œFindITK.cmakeā€ in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by ā€œITKā€, but
CMake did not find one.

Could not find a package configuration file provided by ā€œITKā€ with any of
the following names:

ITKConfig.cmake
itk-config.cmake

Add the installation prefix of ā€œITKā€ to CMAKE_PREFIX_PATH or set ā€œITK_DIRā€
to a directory containing one of the above files. If ā€œITKā€ provides a
separate development package or SDK, be sure it has been installed.

cmake_minimum_required(VERSION 3.16.3)
project(exampleSITK)
set(BUILD_TESTING OFF)
set(BUILD_EXAMPLES OFF)
include(FetchContent)

# Fetch ITK
FetchContent_Declare(
  ITK
  GIT_REPOSITORY https://github.com/InsightSoftwareConsortium/ITK.git
  GIT_TAG        v5.3.0 
)

FetchContent_MakeAvailable(ITK)

# Fetch SimpleITK
FetchContent_Declare(
  SimpleITK
  GIT_REPOSITORY https://github.com/SimpleITK/SimpleITK.git
  GIT_TAG        v2.1.1 
)

FetchContent_MakeAvailable(ITK)
FetchContent_MakeAvailable(SimpleITK)


find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
find_package(SimpleITK REQUIRED)
add_executable(exampleSITK OpenImage.cpp)

target_link_libraries(exampleSITK
  ${SimpleITK_LIBRARIES}
  ${ITK_LIBRARIES}
)

You probable need to do the equivalent of the above for ITK, before invoking MakeAvailble(SimpleITK).

Thanks for the reply. It still does not work. Now I am trying to use ā€œgit submoduleā€ to include simpleITK in the project.