0

I want to include a third package (VTK) in my project. The folder is:

Thirdparty
    VTK
        bin
            Debug
                xxxd.dll
                ...
            Release
                xxx.dll
                ...
        include
                xxx
                ...
        lib
            Debug
                xxx
                ...
            Release
                ...

In this package (VTK), there is d suffix in the dll of debug. Just like: vtkCommonCore-9.0d.dll (debug) vs vtkCommonCore9.0dll (release).

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, because CMake cannot find vtkCommonCore-9.0d.dll in Thirdparty/VTK/bin/Release.

How can I implement the correct CMakeLists.txt for this package (VTK)?

Any suggestion is appreciated!


Update:

After reading the explanation of target_link_libraries, I find a solution for my problem:

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

However, this method seem to be inconvenient.

target_link_libraries provides two method:

  1. A plain library name

    I use it as:

    target_link_libraries(Pro -lvtkCommonCore)
    

    And it failed.

  2. A generator expression I use it as:

    target_link_libraries(Pro vtkCommonCore-9.0$<1:d;2:"">)
    

    And it failed.

I don't know how to use the above methods.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Qiang Zhang
  • 820
  • 8
  • 32
  • You should use `find_file` to find libraries. You can tell CMake that the library might be named with the d suffix. Once the library is found, you just have to pass variables or even targets. See for example https://stackoverflow.com/a/41909627/2799037 – usr1234567 Oct 28 '21 at 06:07
  • 1
    Instead of doing the find_library manually you should follow the recipes described in the VTK examples (see https://kitware.github.io/vtk-examples/site/Cxx/GeometricObjects/CylinderExample/) – vre Oct 28 '21 at 06:22
  • @vre The VTK example requires that the VTK is bulit in my computer. Now, I don't want to build VTK in my computer. I want to include bin/include/lib in my computer. Thus, the find_package would be failed. – Qiang Zhang Oct 28 '21 at 06:30
  • @usr1234567 Do you mean find_library? When I use `find_library(CommonCoreLIb vtkCommonCore)`, the cmake show `CommonCoreLib-NOTFOUND`. Could you please give more details? – Qiang Zhang Oct 28 '21 at 06:37
  • The example should also work whithout VTK needing to be built on your computer. But it need to be installed. For using `find_package` you need to add either the VTK installation directory to your `CMAKE_PREFIX_PATH` or set the variable `VTK_DIR` to this path **before** doing the `find_package` call. Your CMakeLists.txt fragment incorrectly uses `VTI_DIR` (note the typo). – vre Oct 28 '21 at 07:29
  • What do you want to achieve by the generator expression `$<1:d;2:"">`? For me it has no sense. If you want to construct expression "Choose 'd' for debug build and empty string otherwise", then use `$<$:d>`. This kind of expression is provided as an example for conditional expressions: https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html#conditional-expressions. – Tsyvarev Oct 28 '21 at 07:52
  • @Tsyvarev Thank you very much. `$<$:d>` works. Do you know how to use the `A plain library name`? `target_link_libraries(Pro -lvtkCommonCore)` do not work. – Qiang Zhang Oct 28 '21 at 09:19
  • "A plain library name" means that the library will be **searched by the linker** using this name. E.g. with `target_link_libraries(Pro vtkCommonCore)` the linker will search the library with name `vtkCommonCore`. The library will be searched only in the specific directories. E.g. for search under `VTK/bin/Release` you could use `target_link_directories(Pro ${VTK_DIR}/bin/Release)`. Note, that in CMake projects using `target_link_directories` in combination with a plain library name is **discouraged**: instead specify full path to the library file. – Tsyvarev Oct 28 '21 at 09:37

0 Answers0