Extra CMAKE_CXX_FLAGS in UseVTK.cmake cause build errors in CMake projects using VTK
This issue was created automatically from an original Mantis Issue. Further discussion may take place here.
I'm experiencing a CMake problem caused by UseVTK.cmake (VTK version 6.3 release). I have a large CMake project with multiple targets. Some use VTK, some use PCL (which uses VTK). One uses neither, but does use the Nvidia CUDA package. Here is what happens:
- find_package(PCL) in my CMake code automatically runs PCLConfig.cmake.
- PCLConfig.cmake includes UseVTK.cmake after find_package(VTK) is successful (note: this is the intended use, I'm assuming there is nothing wrong with this).
- UseVTK.cmake adds a trainload of flags to CMAKE_C_FLAGS and CMAKE_CXX_FLAGS CMake variables directly (Something wrong with this schema -- discussed later).
- Even though a certain target does not link/use VTK or PCL, the way the CMake default CUDA handling works is that these flags are passed to the nvcc compiler, i.e. here's what gets executed:
---------begin quote----------- /usr/local/cuda/bin/nvcc -M -D__CUDACC__ /home//quickshift_gpu.cu -o /home//reco_segmentation_generated_quickshift_gpu.cu.o.NVCC-depend -ccbin /usr/bin/c++ -m64 -D_DEBUG -DvtkRenderingContext2D_AUTOINIT=1 ( vtkRenderingContextOpenGL ) -DvtkRenderingCore_AUTOINIT=3 ( vtkInteractionStyle,vtkRenderingFreeType,vtkRenderingOpenGL ) -DvtkRenderingVolume_AUTOINIT=1 ( vtkRenderingVolumeOpenGL ) -Dreco_segmentation_EXPORTS -Xcompiler ,"-Wall","-std=c++11","-fPIC","-g" -DNVCC -I/usr/local/cuda/include -I/usr/local/include/vtk-6.3 -I/usr/local/cuda/include ---------end quote-------------
-- instead of simply:
---------begin quote----------- /usr/local/cuda/bin/nvcc /home//quickshift_gpu.cu -c -o /home//reco_segmentation_generated_quickshift_gpu.cu.o -ccbin /usr/bin/c++ -m64 -D_DEBUG -Dreco_segmentation_EXPORTS -Xcompiler ,"-Wall","-std=c++11","-fPIC","-g" -DNVCC -I/usr/local/cuda/include -I/usr/local/cuda/include ---------end quote-------------
Whereas the latter command funs fine, the former fails with "nvcc fatal : A single input file is required for a non-link phase when an outputfile is specified".
Discussion: syntax such as "set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${VTK_REQUIRED_CXX_FLAGS}")" is really not supposed to be used in UseXXX.CMake files, that file is intended for CMake macros/functions(1). Clearly, it causes unintended problems. The modern way to do things like this is to add the flags to something like VTK_CXX_FLAGS, so that the user may use the target_compile_definitions(...) command (2) to add them to specific targets. Ideally, off course, the preprocessor definitions should be set in config header files that are configured by cmake at the time of VTK CMake generation, such as /Common/Core/vtkConfigure.h.in. This way they are defined when the target project includes the headers (which is done any way), and avoid the need for separate target_compile_definitions calls.
-
How do I get around this and get CUDA working on this target without losing PCL/VTK support for other targets? (Currently, I am actually clearing these out via CMake scripting from the COMPILE_DEFINITIONS property of the subproject's directory using the remove_definitions function).
-
Is there any rationale behind setting compiler flags in CMake instead of in header config files? Is there any rationale behind explicitly modifying the CMake flags variables in the UseVTK.cmake script?
[1] https://cmake.org/Wiki/CMake/Tutorials/How_to_create_a_ProjectConfig.cmake_file [2] https://cmake.org/cmake/help/v3.2/command/target_compile_definitions.html