# Undefined Reference error in using simpleitk in C++

**URL:** <https://discourse.itk.org/t/undefined-reference-error-in-using-simpleitk-in-c/2293>\
**Category:** Engineering\
**Tags:** simpleitk\
**Created:** [October 2, 2019, 1:16am UTC](https://discourse.itk.org/t/undefined-reference-error-in-using-simpleitk-in-c/2293 "2019-10-02T01:16:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Felippe\_Trigueiro\_An](https://discourse.itk.org/user_avatar/discourse.itk.org/felippe_trigueiro_an/32/1015_2.png) [@Felippe\_Trigueiro\_An](https://discourse.itk.org/u/Felippe_Trigueiro_An)\
**Post date:** [October 2, 2019, 1:16am UTC](https://discourse.itk.org/t/undefined-reference-error-in-using-simpleitk-in-c/2293/1 "2019-10-02T01:16:23Z")

</div>

I installed the simpleitk in my ubuntu 16.04 as described in this link ([Building simpleITK](https://simpleitk.readthedocs.io/en/master/Documentation/docs/source/building.html#building-simpleitk)) and I’m trying to use with QT creator. The problem is that I’m getting lots of undefined reference error, but I included ALL the .a files contained in the SimpleITK-build/lib and ITK-build/lib folder.

When I try to use the python code, the SimpleItk works fine. So, what am I missing?  
Thanks.

---

<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:** [October 2, 2019, 1:24pm UTC](https://discourse.itk.org/t/undefined-reference-error-in-using-simpleitk-in-c/2293/2 "2019-10-02T13:24:12Z")

</div>

Please using CMake with Qt Creator: [https://doc.qt.io/qtcreator/creator-project-cmake.html](https://doc.qt.io/qtcreator/creator-project-cmake.html)

With CMake to use SimpleITK you simply need to add:

```auto
find_package(SimpleITK REQUIRED)
...
target_link_libraries ( YourExecutable ${SimpleITK_LIBRARIES} )

```

SimpleITK uses modern CMake with target attributes so the required compiler flags and include paths are automatically added. It is possible to create a minimal CMake project, configure it, then copy the build parameters to Qt Creator. However, this is a fragile and unsupported workflow.

You question here is missing the same information necessary for an informed answer what was originally missing from [stack overflow](https://stackoverflow.com/questions/58174562/simpleitk-cannot-find-the-itk-functions-in-c).

---

<div class="post-metadata">

**Author:** ![Felippe\_Trigueiro\_An](https://discourse.itk.org/user_avatar/discourse.itk.org/felippe_trigueiro_an/32/1015_2.png) [@Felippe\_Trigueiro\_An](https://discourse.itk.org/u/Felippe_Trigueiro_An)\
**Post date:** [October 2, 2019, 2:13pm UTC](https://discourse.itk.org/t/undefined-reference-error-in-using-simpleitk-in-c/2293/3 "2019-10-02T14:13:50Z")

</div>

Thanks, Bradley.

So, for compiling I have to use the cmake instead of qmake? I’m asking this because I’m not very familiar with cmake, but if necessary I can learn more.

---

<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:** [October 2, 2019, 6:47pm UTC](https://discourse.itk.org/t/undefined-reference-error-in-using-simpleitk-in-c/2293/4 "2019-10-02T18:47:26Z")

</div>

It is recommended and supported to use CMake. with SimpleITK C++ interface. Time can be spent hacking at unsupported usage, or learning another useful tool.

---

<div class="post-metadata">

**Author:** ![Felippe\_Trigueiro\_An](https://discourse.itk.org/user_avatar/discourse.itk.org/felippe_trigueiro_an/32/1015_2.png) [@Felippe\_Trigueiro\_An](https://discourse.itk.org/u/Felippe_Trigueiro_An)\
**Post date:** [October 4, 2019, 3:53pm UTC](https://discourse.itk.org/t/undefined-reference-error-in-using-simpleitk-in-c/2293/5 "2019-10-04T15:53:43Z")

</div>

Blowekamp, I created a cmakelist file with your recommendation but the cmake returns me some error message:

> CMake Error at /home/felippe/SimpleITK-build/lib/cmake/SimpleITK-1.3/SimpleITKConfig.cmake:67 (find\_package):  
> 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” (requested  
> version 4.13.2) 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.  
> Call Stack (most recent call first):  
> CMakeLists.txt:4 (find\_package)
> 
> Configuring incomplete, errors occurred!  
> See also “/home/felippe/Área de Trabalho/Felippe/Mestrado/C\_plus\_plus/Codigos/build-Registration\_ITK\_CMAKE-Desktop\_Qt\_5\_12\_3\_GCC\_64bit-Default/CMakeFiles/CMakeOutput.log”.  
> CMake Deprecation Warning:  
> The ‘cmake-server(7)’ is deprecated. Please port clients to use the  
> ‘cmake-file-api(7)’ instead.
> 
> CMake Project parsing failed.

So, I did what it was recommended, I set the both (but not in the same time) variables ITK\_DIR and CMAKE\_PREFIX\_PATH, as you can see below:

```
cmake_minimum_required(VERSION 3.10)

project(Registration_ITK_CMAKE)
find_package(SimpleITK REQUIRED)
#set(ITK_DIR "/home/felippe/SimpleITK-build/ITK-prefix/lib/cmake/ITK/")
set(CMAKE_PREFIX_PATH "/home/felippe/SimpleITK-build/ITK-prefix/")
add_executable(${PROJECT_NAME} "main.cpp")
target_link_libraries ( YourExecutable ${SimpleITK_LIBRARIES} )

```

So, what am I missing again?

Thanks.

---

<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:** [October 4, 2019, 4:06pm UTC](https://discourse.itk.org/t/undefined-reference-error-in-using-simpleitk-in-c/2293/6 "2019-10-04T16:06:05Z")

</div>

Hello,

Looks like you are making good progress. I usually set `ITK_DIR` what should have looks right to me. What is the content of `/home/felippe/SimpleITK-build/ITK-prefix/lib/cmake/ITK/`? I have the following:

```auto
 $ls SimpleITK/ITK-prefix/lib/cmake/ITK/
ITKConfig.cmake ITKTargets-release.cmake UseITK.cmake
ITKConfigVersion.cmake ITKTargets.cmake itkImageIOFactoryRegisterManager.h.in
ITKModuleAPI.cmake Modules itkTransformIOFactoryRegisterManager.h.in
```

---

<div class="post-metadata">

**Author:** ![Felippe\_Trigueiro\_An](https://discourse.itk.org/user_avatar/discourse.itk.org/felippe_trigueiro_an/32/1015_2.png) [@Felippe\_Trigueiro\_An](https://discourse.itk.org/u/Felippe_Trigueiro_An)\
**Post date:** [October 4, 2019, 4:09pm UTC](https://discourse.itk.org/t/undefined-reference-error-in-using-simpleitk-in-c/2293/7 "2019-10-04T16:09:32Z")

</div>

I have this:

```
felippe@felippe-pc:~/SimpleITK-build/ITK-prefix/lib/cmake/ITK$ ls
ITKConfig.cmake
ITKConfigVersion.cmake
itkImageIOFactoryRegisterManager.h.in
ITKModuleAPI.cmake
ITKTargets.cmake
ITKTargets-release.cmake
itkTransformIOFactoryRegisterManager.h.in
Modules
UseITK.cmake

```

It seems the same…

I’m compiling using cmake together with QT (qt has an option of compiling using cmake instead of the qmake).

---

<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:** [October 4, 2019, 4:28pm UTC](https://discourse.itk.org/t/undefined-reference-error-in-using-simpleitk-in-c/2293/8 "2019-10-04T16:28:41Z")

</div>

You need to set ITK\_DIR before calling `find_package(SimpleITK REQUIRED)`.

I usual set ITK\_DIR and SimpleITK\_DIR on the command line during configuration like this:

```auto
-DSimpleITK_DIR=/scratch/blowekamp/SimpleITK/lib/cmake/SimpleITK-1.3/ -DITK_DIR=/scratch/blowekamp//SimpleITK/ITK-prefix/lib/cmake/ITK/ .

```

---

<div class="post-metadata">

**Author:** ![Felippe\_Trigueiro\_An](https://discourse.itk.org/user_avatar/discourse.itk.org/felippe_trigueiro_an/32/1015_2.png) [@Felippe\_Trigueiro\_An](https://discourse.itk.org/u/Felippe_Trigueiro_An)\
**Post date:** [October 4, 2019, 4:37pm UTC](https://discourse.itk.org/t/undefined-reference-error-in-using-simpleitk-in-c/2293/9 "2019-10-04T16:37:39Z")

</div>

Problem Solved. Thank you very much blowekamp, you saved me!!!
