VTK find_package fails with includes

Hi, I am facing an issue with the includes in my code.
In my CMakeLists.txt I first create a library, then an executable which include vtk headers.
If I remove the add_library (so far my executable does not depend on it), there is no issue, my executable is produced.
Otherwise, running make fails with first vtk header included “vtkActor.h” no such file… Weirdly, If i place my main.cpp aside my CMakeLists.txt no issue encountered.

So far, my fix has been to create the variable VTK_INCLUDE_DIRS and to get all vtk include directories.

Personally, I think it is a shame that the VTK_INCLUDE_DIRS is not filled automatically with the command find_package …

Your comments are most welcome !

VTK_INCLUDE_DIRS is no longer needed. Just link to the appropriate library and the include directories come in automatically. For vtkActor.h, you would want the VTK::RenderingCore library.

I know, I have actually based my CMakeLists.txt on the one in the example.
I have reproduced the bug within this example. Say I use add_library(…) before add_executable and the error with the includes arise. As I said I moved the main.cpp to src/apps because when it stays aside the CMakeLists.txt the issue disappears…

So this looks very much like a bug ?
Here is my CMakeLists.txtCMakeLists.txt (1.9 KB)

Anyway, I think, filling the VTK_INCLUDE_DIRS by the find_package provides more freedom and is more custom …

VTK_INCLUDE_DIRS is gone and I’m not bringing it back because it shouldn’t be needed. Its usage would just be papering over other problems. Can you post the actual error you’re getting?

nparent@paradis:/scratch/nparent/Formations/vtk/MyExample/build$ make
Scanning dependencies of target knitsim
[ 25%] Building CXX object CMakeFiles/knitsim.dir/src/apps/main.cpp.o
/scratch/nparent/Formations/vtk/MyExample/src/apps/main.cpp:1:10: fatal error: vtkActor.h: No such file or directory
#include <vtkActor.h>
^~~~~~~~~~~~
compilation terminated.
CMakeFiles/knitsim.dir/build.make:62: recipe for target ‘CMakeFiles/knitsim.dir/src/apps/main.cpp.o’ failed
make[2]: *** [CMakeFiles/knitsim.dir/src/apps/main.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target ‘CMakeFiles/knitsim.dir/all’ failed
make[1]: *** [CMakeFiles/knitsim.dir/all] Error 2
Makefile:83: recipe for target ‘all’ failed
make: *** [all] Error 2

Can out send make VERBOSE=1 output to show the command line that gets generated?

Have you download the tar file in PolyLine and built it? This should build with no issues. In the CMakeLists.txt file include(${VTK_USE_FILE}) is only applicable to VTK versions less then 8.90.

Use VTK_LIBRARIES instead. You can check what libraries will be used by:

message(STATUS "VTK Libraries: ${VTK_LIBRARIES}")

I just had a look at your CmakeLists.txt file, in particular add_executable(Writer MACOSX_BUNDLE src/apps/main.cpp ). I don’t know your directory layout but CMake expects absolute paths so you should be expressing the path to main.cpp relative to ${CMAKE_CURRENT_SOURCE_DIR} , which is the directory containing the current CMakeLists.txt file e.g perhaps:

${CMAKE_CURRENT_SOURCE_DIR}/src/apps/main.cpp

With regard to directory layout and CMake files it may be worth looking at something like this: Simple Modern CMake Tutorial Part 1 or An Introduction to Modern CMake.

Here it is :

nparent@paradis:/scratch/nparent/Formations/vtk/MyExample/build$ make VERBOSE=1
/usr/bin/cmake -H/scratch/nparent/Formations/vtk/MyExample -B/scratch/nparent/Formations/vtk/MyExample/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /scratch/nparent/Formations/vtk/MyExample/build/CMakeFiles /scratch/nparent/Formations/vtk/MyExample/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1] : on entre dans le répertoire « /scratch/nparent/Formations/vtk/MyExample/build »
make -f CMakeFiles/knitsim.dir/build.make CMakeFiles/knitsim.dir/depend
make[2] : on entre dans le répertoire « /scratch/nparent/Formations/vtk/MyExample/build »
cd /scratch/nparent/Formations/vtk/MyExample/build && /usr/bin/cmake -E cmake_depends “Unix Makefiles” /scratch/nparent/Formations/vtk/MyExample /scratch/nparent/Formations/vtk/MyExample /scratch/nparent/Formations/vtk/MyExample/build /scratch/nparent/Formations/vtk/MyExample/build /scratch/nparent/Formations/vtk/MyExample/build/CMakeFiles/knitsim.dir/DependInfo.cmake --color=
make[2] : on quitte le répertoire « /scratch/nparent/Formations/vtk/MyExample/build »
make -f CMakeFiles/knitsim.dir/build.make CMakeFiles/knitsim.dir/build
make[2] : on entre dans le répertoire « /scratch/nparent/Formations/vtk/MyExample/build »
[ 25%] Building CXX object CMakeFiles/knitsim.dir/src/apps/main.cpp.o
/usr/bin/c++ -DEIGEN_DONT_PARALLELIZE -DEIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET -Dknitsim_EXPORTS -std=c++0x -fPIC -msse3 -Wno-unused -Wno-enum-compare -fPIC -o CMakeFiles/knitsim.dir/src/apps/main.cpp.o -c /scratch/nparent/Formations/vtk/MyExample/src/apps/main.cpp
/scratch/nparent/Formations/vtk/MyExample/src/apps/main.cpp:1:10: fatal error: vtkActor.h: Aucun fichier ou dossier de ce type
#include <vtkActor.h>
^~~~~~~~~~~~
compilation terminated.
CMakeFiles/knitsim.dir/build.make:62: recipe for target ‘CMakeFiles/knitsim.dir/src/apps/main.cpp.o’ failed
make[2]: *** [CMakeFiles/knitsim.dir/src/apps/main.cpp.o] Error 1
make[2] : on quitte le répertoire « /scratch/nparent/Formations/vtk/MyExample/build »
CMakeFiles/Makefile2:67: recipe for target ‘CMakeFiles/knitsim.dir/all’ failed
make[1]: *** [CMakeFiles/knitsim.dir/all] Error 2
make[1] : on quitte le répertoire « /scratch/nparent/Formations/vtk/MyExample/build »
Makefile:83: recipe for target ‘all’ failed
make: *** [all] Error 2

Thanks for looking at it, but it worked with the relative path. But clearly a bad habit !

So thanks a lot everybody for helping me out.

Week-end rest gave me the answer. It happens that in the add_library I used a file( GLOB RECURSE …)
which searches in the Apps folder. In this Apps folder, are the files with main function for which I want to produce an exectuable and they are then liked with the library.

So my fix, is to put this folder somewhere else so that those files are now sources of the library.

Glad you solved it! The links I mentioned will help.

This is just false. CMake looks up source files relative to ${CMAKE_CURRENT_SOURCE_DIR} when they are relative automatically.

It seems the issue was that there was a file(GLOB_RECURSE) laying around (IMO, a bad habit for listing source files), not anything with a path specification.