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

**URL:** https://discourse.itk.org/t/how-to-use-fetchcontent-in-cmakelist-txt-to-download-build-simpleitk-and-create-example/7231
**Category:** Beginner Questions
**Tags:** simpleitk
**Created:** [September 19, 2024, 8:45pm UTC](https://discourse.itk.org/t/how-to-use-fetchcontent-in-cmakelist-txt-to-download-build-simpleitk-and-create-example/7231 "2024-09-19T20:45:21Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![SuperTXUH](https://discourse.itk.org/user_avatar/discourse.itk.org/supertxuh/32/4360_2.png) [@SuperTXUH](https://discourse.itk.org/u/SuperTXUH)
#### Post date: [September 19, 2024, 8:45pm UTC](https://discourse.itk.org/t/how-to-use-fetchcontent-in-cmakelist-txt-to-download-build-simpleitk-and-create-example/7231/1 "2024-09-19T20:45:21Z")

</div>

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

```auto
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?

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [September 20, 2024, 1:33pm UTC](https://discourse.itk.org/t/how-to-use-fetchcontent-in-cmakelist-txt-to-download-build-simpleitk-and-create-example/7231/2 "2024-09-20T13:33:45Z")

</div>

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](https://discourse.cmake.org/t/is-it-normal-to-resort-to-using-superbuilds/9359).

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [September 20, 2024, 1:39pm UTC](https://discourse.itk.org/t/how-to-use-fetchcontent-in-cmakelist-txt-to-download-build-simpleitk-and-create-example/7231/3 "2024-09-20T13:39:23Z")

</div>

The documentation for building SimpleITK is here:  
[https://simpleitk.readthedocs.io/en/master/building.html](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.

---

<div class="post-metadata">

### Author: ![SuperTXUH](https://discourse.itk.org/user_avatar/discourse.itk.org/supertxuh/32/4360_2.png) [@SuperTXUH](https://discourse.itk.org/u/SuperTXUH)
#### Post date: [September 20, 2024, 3:26pm UTC](https://discourse.itk.org/t/how-to-use-fetchcontent-in-cmakelist-txt-to-download-build-simpleitk-and-create-example/7231/4 "2024-09-20T15:26:06Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![SuperTXUH](https://discourse.itk.org/user_avatar/discourse.itk.org/supertxuh/32/4360_2.png) [@SuperTXUH](https://discourse.itk.org/u/SuperTXUH)
#### Post date: [September 20, 2024, 3:31pm UTC](https://discourse.itk.org/t/how-to-use-fetchcontent-in-cmakelist-txt-to-download-build-simpleitk-and-create-example/7231/5 "2024-09-20T15:31:57Z")

</div>

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.

```auto
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}
)

```

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [September 20, 2024, 3:39pm UTC](https://discourse.itk.org/t/how-to-use-fetchcontent-in-cmakelist-txt-to-download-build-simpleitk-and-create-example/7231/6 "2024-09-20T15:39:30Z")

</div>

> [@SuperTXUH](#):
>
> `set(SimpleITK_DIR ${SimpleITK_BINARY_DIR} CACHE STRING "Make the SimpleITK_DIR available to other modules" FORCE)`

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

---

<div class="post-metadata">

### Author: ![SuperTXUH](https://discourse.itk.org/user_avatar/discourse.itk.org/supertxuh/32/4360_2.png) [@SuperTXUH](https://discourse.itk.org/u/SuperTXUH)
#### Post date: [September 23, 2024, 3:58pm UTC](https://discourse.itk.org/t/how-to-use-fetchcontent-in-cmakelist-txt-to-download-build-simpleitk-and-create-example/7231/7 "2024-09-23T15:58:08Z")

</div>

Thanks for the reply. It still does not work. Now I am trying to use “git submodule” to include simpleITK in the project.
