Issue
Trying out a CMAKE Project in Visual Studio 2022 for the first time and I am trying to adapt CmakeLists.txt
rel="nofollow noreferrer">from this repository in order to use the Open3D library.
As far as I understand, the following section means that on windows and in case Open3D was build as shared library, then the .dll file should be copied to the output folder of the project.
if(WIN32)
get_target_property(open3d_type Open3D::Open3D TYPE)
if(open3d_type STREQUAL "SHARED_LIBRARY")
message(STATUS "Copying Open3D.dll to ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>")
add_custom_command(TARGET Draw POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_INSTALL_PREFIX}/bin/Open3D.dll
${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>)
endif()
endif()
However, on my machine the variable ${CMAKE_INSTALL_PREFIX}
points to <project-root>/out/install/x64-debug
which seems to be generated from VS2022(?).
What I have done
So far I have added system variable to my windows environment variables called OPEN3D_DIR
, which points to the root of the open3d binaries (=the folder containing bin, CMake, include and lib folder of the library) With that find_package(OPEN3D REQUIRED)
seems to work, however in the script, the variable ${OPEN3D_DIR}
points to <OPEN3D-root>/CMake
Basically, in short my question is how finding package paths is normally handled, where does the value for ${CMAKE_INSTALL_PREFIX}
come from and in general, how would one set variables, which would normally be set in the CMAKE GUI?
Solution
You can use $<TARGET_FILE:Open3D::Open3D>
to get the path to the DLL from the target, you can also simplify the output directory to $<TARGET_FILE_DIR:Draw>
copy_if_different
might be better than copy
to make your build a bit faster.
e.g.:
if(WIN32)
get_target_property(open3d_type Open3D::Open3D TYPE)
if(open3d_type STREQUAL "SHARED_LIBRARY")
message(STATUS "Copying Open3D.dll to $<TARGET_FILE_DIR:Draw>")
add_custom_command(TARGET Draw POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:Open3D::Open3D>
$<TARGET_FILE_DIR:Draw>)
endif()
endif()
See https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html for more useful expressions.
Answered By - Alan Birtles Answer Checked By - Pedro (WPSolving Volunteer)