How to link library with suffix d for debug?

I want to use a third package (VTK) in my project, and the folder is:

Thirdparty
    VTK
        bin
            Debug
                vtkCommonCore-9.0d.dll
                ...
            Release
                vtkCommonCore-9.0.dll
                ...
        include
                xxx
                ...
        lib
            Debug
                vtkCommonCore-9.0d.lib
                ...
            Release
                vtkCommonCore-9.0.dll
                ...

In my CMakeLists.txt:

set(VTI_DIR "xxx/Thirdparty/VTK")
include_directories(
    ${VTK_DIR}/include
)
target_link_libraries(Pro
    vtkCommonCore-9.0
    ....
)

If I use target_link_libraries(Pro vtkCommonCore-9.0d ....) , the code would be ok for debug but wrong for release.
If I use target_link_libraries(Pro vtkCommonCore-9.0 ....) , the code would be ok for release but wrong for debug.

One solution is:

target_link_libraries(Pro
    debug vtkCommonCore-9.0d
    optimized vtkCommonCore-9.0
    ....
)

But, it seem to be inconvenient.

Is there any method which is more convenient?

Yes. Use find_package(VTK) and link to VTK::CommonCore instead.

If I want to use fidn_package(VTK), I need to compile vtk in my computer. But, I don’t want to build vtk in my computer, and I just want to use vtk by bin/lib/include.

How are you going to use it if you don’t compile it? Where does libvtkCommonCore-9.0.lib come from if you haven’t built it?

I can build vtk in one compute, and install vtk. Thus, I can obtain the dll/lib/include files.

Then, I copy thus files to another computer. In this new computer, the vtk is not built, and the find_package will fail.

I want to use vtk by only dll/lib, rather that to build it.

Why do you not pack the hole installation packages of vtk?

/usr/local/lib/cmake/vtk-9.0/
β”œβ”€β”€ FindEXPAT.cmake
β”œβ”€β”€ FindEigen3.cmake
β”œβ”€β”€ FindFFMPEG.cmake
β”œβ”€β”€ FindFontConfig.cmake
β”œβ”€β”€ FindFreetype.cmake
β”œβ”€β”€ FindGL2PS.cmake
β”œβ”€β”€ FindGLEW.cmake
β”œβ”€β”€ FindJOGL.cmake
β”œβ”€β”€ FindJsonCpp.cmake
β”œβ”€β”€ FindLZ4.cmake
β”œβ”€β”€ FindLZMA.cmake
β”œβ”€β”€ FindLibHaru.cmake
β”œβ”€β”€ FindLibPROJ.cmake
β”œβ”€β”€ FindLibXml2.cmake
β”œβ”€β”€ FindMySQL.cmake
β”œβ”€β”€ FindNetCDF.cmake
β”œβ”€β”€ FindODBC.cmake
β”œβ”€β”€ FindOGG.cmake
β”œβ”€β”€ FindOSMesa.cmake
β”œβ”€β”€ FindOpenMP.cmake
β”œβ”€β”€ FindOpenSlide.cmake
β”œβ”€β”€ FindOpenVR.cmake
β”œβ”€β”€ FindPEGTL.cmake
β”œβ”€β”€ FindTBB.cmake
β”œβ”€β”€ FindTHEORA.cmake
β”œβ”€β”€ Finddouble-conversion.cmake
β”œβ”€β”€ Findmpi4py.cmake
β”œβ”€β”€ Findutf8cpp.cmake
β”œβ”€β”€ VTK-targets-release.cmake
β”œβ”€β”€ VTK-targets.cmake
β”œβ”€β”€ VTK-vtk-module-find-packages.cmake
β”œβ”€β”€ VTK-vtk-module-properties.cmake
β”œβ”€β”€ VTKPython-targets-release.cmake
β”œβ”€β”€ VTKPython-targets.cmake
β”œβ”€β”€ patches
β”‚   β”œβ”€β”€ 3.13
β”‚   β”‚   └── FindZLIB.cmake
β”‚   β”œβ”€β”€ 3.16
β”‚   β”‚   β”œβ”€β”€ FindMPI
β”‚   β”‚   β”‚   β”œβ”€β”€ fortranparam_mpi.f90.in
β”‚   β”‚   β”‚   β”œβ”€β”€ libver_mpi.c
β”‚   β”‚   β”‚   β”œβ”€β”€ libver_mpi.f90.in
β”‚   β”‚   β”‚   β”œβ”€β”€ mpiver.f90.in
β”‚   β”‚   β”‚   β”œβ”€β”€ test_mpi.c
β”‚   β”‚   β”‚   └── test_mpi.f90.in
β”‚   β”‚   β”œβ”€β”€ FindMPI.cmake
β”‚   β”‚   └── FindPostgreSQL.cmake
β”‚   β”œβ”€β”€ 3.18
β”‚   β”‚   β”œβ”€β”€ FindPython
β”‚   β”‚   β”‚   └── Support.cmake
β”‚   β”‚   β”œβ”€β”€ FindPython2.cmake
β”‚   β”‚   └── FindPython3.cmake
β”‚   └── 99
β”‚       β”œβ”€β”€ FindGDAL.cmake
β”‚       β”œβ”€β”€ FindHDF5.cmake
β”‚       β”œβ”€β”€ FindJPEG.cmake
β”‚       β”œβ”€β”€ FindLibArchive.cmake
β”‚       β”œβ”€β”€ FindOpenGL.cmake
β”‚       β”œβ”€β”€ FindSQLite3.cmake
β”‚       └── FindX11.cmake
β”œβ”€β”€ vtk-config-version.cmake
β”œβ”€β”€ vtk-config.cmake
β”œβ”€β”€ vtk-prefix.cmake
β”œβ”€β”€ vtk-use-file-compat.cmake
β”œβ”€β”€ vtk-use-file-deprecated.cmake
β”œβ”€β”€ vtk-use-file-error.cmake
β”œβ”€β”€ vtkCMakeBackports.cmake
β”œβ”€β”€ vtkDetectLibraryType.cmake
β”œβ”€β”€ vtkEncodeString.cmake
β”œβ”€β”€ vtkHashSource.cmake
β”œβ”€β”€ vtkModule.cmake
β”œβ”€β”€ vtkModuleGraphviz.cmake
β”œβ”€β”€ vtkModuleJson.cmake
β”œβ”€β”€ vtkModuleTesting.cmake
β”œβ”€β”€ vtkModuleWrapJava.cmake
β”œβ”€β”€ vtkModuleWrapPython.cmake
β”œβ”€β”€ vtkObjectFactory.cmake
β”œβ”€β”€ vtkObjectFactory.cxx.in
β”œβ”€β”€ vtkObjectFactory.h.in
β”œβ”€β”€ vtkTestingDriver.cmake
β”œβ”€β”€ vtkTestingRenderingDriver.cmake
β”œβ”€β”€ vtkTopologicalSort.cmake
└── vtkmodules-vtk-python-module-properties.cmake

7 directories, 76 files
Claus-iMac:~ clausklein$ 

As stated above, you should copy the whole install tree. That will come with what you need to find_package(VTK). This includes the vtk_module_autoinit command you’ll need to use if you use any object factories (commonly, rendering or MPI support).

Yes, I have copied the whole install tree. Now, the folder is:

Thirdparty
    VTK
        bin
            Debug
                vtkCommonCore-9.0d.dll
                ...
            Release
                vtkCommonCore-9.0.dll
                ...
        include
                xxx
                ...
        lib
            Debug
                cmake
                    vtk-9.0
                        patches
                        Finddouble-comversion.cmake
                        ....
                        vtk-config.cmake
                        ...
                        VTK-targets-debug.cmake
                        ...
                vtkCommonCore-9.0d.lib
                ...
            Release
                cmake
                    vtk-9.0
                        patches
                        Finddouble-comversion.cmake
                        ....
                        vtk-config.cmake
                        ...
                        VTK-targets-release.cmake
                        ...
                vtkCommonCore-9.0.dll
                ...

But, if I use find_package(VTK) as:

set(VTK_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Thirdparty/VTK")
find_package(VTK)

The cmake will report :

By not providing "FIndVTK.cmake" in CMAKE_MODULE_PATH this project has asked CMAke to find a package configuration file provided by "VTK", but CMAke did not find one.

Could not find a package configuration file provided by "VTK" with any of the following names:

VTKConfig.cmake
vtk-config.cmake

Add the installation prefix of "VTK" to CMAKE_PREFIX_PATH or set "VTK_DIR" to a directory containing one of the above files. If "VTK" provided a separate development package or SDK, be sure it  has been installed. 

If my following cmake file is wrong?

set(VTK_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Thirdparty/VTK")
find_package(VTK)

How can I implement my cmake file to make find_package work?

You can either set CMAKE_PREFIX_PATH to the install root of VTK or VTK_DIR to the directory that contains vtk-config.cmake.

There are two paths contains vtk-config.cmake.

  1. Thirdparty/VTK/lib/Debug/cmake/vtk-9.0
  2. Thirdparty/VTK/lib/Release/cmake/vtk-9.0

Whick path should be VTK_DIR?

How did you get such a setup? The CMake files should never be under a config-specific location unless you specified that manually. If you want a single install prefix with both, you’ll need a single CMake package directory which knows about both.

Yes, I manually copy the dll/lib/include files to a config-specific location. The reason is that I want to include Debug/Release/RelWithDebInfo files.

If I do not copy dll/lib/include files to a config-specific location manually, I can distinguish the debug/release by suffix. But, how can I distinguish lib of release and RelWithDebInfo?

Right now, VTK doesn’t have a different suffix for release and relwithdebinfo built-in. You can pass -DCMAKE_RELWITHDEBINFO_POSTFIX=rd to the configure to make a different suffixes for that configuration. Then you should be able to have then entire setup in a unified config-less install tree.

My folder is:

Thirdparty
    x64
        VTK
            bin
            include
            lib
                cmake
                    vtk-9.0
                        vtk-config.cmake
                        ...

And My Cmakelists.txt is:

set(VTK_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Thirdparty/$(PlatformName)/VTK/lib/cmake/vtk-9.0")
find_package(VTK REQUIRED)

But, it still reported:

By not providing "FIndVTK.cmake" in CMAKE_MODULE_PATH this project has asked CMAke to find a package configuration file provided by "VTK", but CMAke did not find one.

Could not find a package configuration file provided by "VTK" with any of the following names:

VTKConfig.cmake
vtk-config.cmake

Add the installation prefix of "VTK" to CMAKE_PREFIX_PATH or set "VTK_DIR" to a directory containing one of the above files. If "VTK" provided a separate development package or SDK, be sure it  has been installed. 

Then, I print the VTK_DIR, it shows:

Project/Thirdparty/$(PlatformName)/VTK/lib/cmake/vtk-9.0

Is there any wrong with my VTK_DIR by: set(VTK_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Thirdparty/$(PlatformName)/VTK/lib/cmake/vtk-9.0")?

But, the following is OK:

set(VTK_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Thirdparty/x64/VTK/lib/cmake/vtk-9.0")

CMake will take this literally, so it will never see x64. You’ll need to compute this based on whatever information you have and set a CMake variable appropriately.

1 Like

thank you for your suggestions~~~