link error. ld: symbol(s) not found for architecture x86_64

I’m learning the examples provided by ITK(https://itk.org/ITKExamples/src/index.htm)
when I’m trying the first example( Convert An itk::Image To vtkImageData)(https://itk.org/ITKExamples/src/Bridge/VtkGlue/ConvertAnitkImageTovtkImageData/Documentation.html)
there is no CMakeLists.txt in this web page, but I have found one in https://github.com/InsightSoftwareConsortium/ITKExamples/blob/master/src/Bridge/VtkGlue/ConvertAnitkImageTovtkImageData/CMakeLists.txt
so I wrote my own CMakeLists.txt like this

cmake_minimum_required(VERSION 3.17)
project(ConvertAnitkImageTovtkImageData)
set(CMAKE_CXX_STANDARD 11)
# and I add the next two lines , otherwise it told me can't find .h files
include_directories("/usr/local/include/ITK-5.1")
include_directories("/usr/local/include/vtk-9.0")
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
add_executable(ConvertAnitkImageTovtkImageData Code.cxx)
target_link_libraries(ConvertAnitkImageTovtkImageData ${ITK_LIBRARIES})

It’s Ok when I ran cmake. But an error occurred just like the title.



hope the image is useful.

Also I wanna ask , I have tried the helloWorld project like this
https://itk.org/ITKExamples/src/Core/Common/BuildAHelloWorldProgram/Documentation.html
it works good with the “raw” CMakeLists.txt (without include_directories(XXX) )
but when I try to compile it without cmake , just like this
" g++ helloWorld.cxx -o helloWorld -std=c++11 "
an error occurred that “itkImage.h not found”
and I tried again " g++ helloWorld.cxx -o HelloWorld -std=c++11 -I /usr/local/include/ITK-5.1 "
there is the same error as the images show

Did you try adding find/link VTK?

cmake_minimum_required(VERSION 3.17)
project(ConvertAnitkImageTovtkImageData)
set(CMAKE_CXX_STANDARD 11)
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
add_executable(ConvertAnitkImageTovtkImageData Code.cxx)
target_link_libraries(ConvertAnitkImageTovtkImageData ${ITK_LIBRARIES} ${VTK_LIBRARIES})

Yeah I have tried it. And it makes no difference
I’m now thinking about whether it’s because cmake has found a wrong path(for I have ever installed vmtk which uses VTK-8.1 and I compiled VTK-9.0)
Did you know how to assign a fixed path?
I have tried to use like these
set(VTK_DIR , “XXX”)
set(CMAKE_INCLUDE_PATH “XXX”)
find_package(ITK PATH_SUFFIXES “XXX”)
but it seems that cmake always first find VTK from vmtk , only if I do this
find_package(VTK 9.0 XXXX)
can cmake find the right one

Well…After I typed the words above I tried again with adding find/link VTK and set it’s VERSION , and it worked. Thanks a lot for your help !

Maybe include the final code which worked for you, in case someone has a similar problem in the future?

You should do find ITK first, because it should get the correct version of VTK it was built against. Then do find VTK - it should get the same version from ITK. I think this is the correct order. The order was important some years ago, and might still be.

Yeah it will be good if I can help somebody else , while I’m not good at using cmake so this may not be a good example , but anyway , here is my “CMakeLists.txt”

# CMakeLists.txt
cmake_minimum_required(VERSION 3.17)
project(ConvertAnitkImageTovtkImageData)
set(CMAKE_CXX_STANDARD 11)
find_package(ITK 5.1 REQUIRED)
include("${ITK_USE_FILE}")
find_package(VTK 9.0 REQUIRED)
add_executable(ConvertAnitkImageTovtkImageData ConvertAnitkImageTovtkImageData.cxx)
target_link_libraries(ConvertAnitkImageTovtkImageData ${ITK_LIBRARIES})
# VTK 9.0 recommend this , instead of (include("${VTK_USE_FILE}"))
vtk_module_autoinit(TARGETS ConvertAnitkImageTovtkImageData 
                                 MODULES ${VTK_LIBRARIES})

I just figure out the VERSION , but I think this will be a better way to figure out the path of the package

 find_package(ITK 
    PATHS "/usr/local/XXXXX"
    NO_DEFAULT_PATH)
1 Like