Can't use a library installed with VCPKG in CLion
Asked Answered
T

1

12

I followed the tutorial described in the VCPKG github site and then installed OpenMesh 8.0, and after, i linked the toolchain

-DCMAKE_TOOLCHAIN_FILE=/home/diolante/vcpkg/scripts/buildsystems/vcpkg.cmake 

in Clion toolchain settings and when I reload the CMakeLists.txt that I changed in the Clion project:

# CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(lul)

find_package(openmesh REQUIRED)

add_executable(main main.cpp)
target_link_libraries(main openmesh)

The following error returns at Clion output:

CMake Error at /home/diolante/vcpkg/scripts/buildsystems/vcpkg.cmake:288 (_find_package):   By not providing "Findopenmesh.cmake" in CMAKE_MODULE_PATH this project has   asked CMake to find a package configuration file provided by "openmesh",   but CMake did not find one.

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

    openmeshConfig.cmake
    openmesh-config.cmake

  Add the installation prefix of "openmesh" to CMAKE_PREFIX_PATH or set
  "openmesh_DIR" to a directory containing one of the above files. If
  "openmesh" provides a separate development package or SDK, be sure it has
  been installed.
Toothy answered 14/3, 2020 at 15:4 Comment(0)
P
7

The following is based on Visual Studio compilers but should be easily adoptable to different environments.

First you need to install the triplet you are going to use, e.g.

vcpkg install openmesh --triplet x64-windows --triplet x86-windows

(If you are not sure whether to build your project x64 or x86 simply install both triplets.)

Select the proper toolchain in the "Toolchains" entry under "Build, Execution, Deployment". To make a toolchain default move it to the first entry in the list by using the small up and down arrows.

Toolchain Selection

Select the toolchain in the "CMake" entry under "Build, Execution, Deployment"

ToolchainSelection2

If you selected a toolchain/architecture for which the OpenMesh triplet has not been installed you will see exactly the error you are experiencing.

vcpkg provides two variables for OpenMesh 8.1 that need to be used to specify the include directories and the libraries to link with as follows:

target_include_directories(main PRIVATE ${OPENMESH_INCLUDE_DIRS})
target_link_libraries(main PRIVATE ${OPENMESH_LIBRARIES})

The entire CMakeLists.txt for this project now would look like this:

cmake_minimum_required(VERSION 3.14)
project(lul)

find_package(openmesh REQUIRED)

add_executable(main main.cpp)
target_include_directories(main PRIVATE ${OPENMESH_INCLUDE_DIRS})
target_link_libraries(main PRIVATE ${OPENMESH_LIBRARIES})
# the following line is only needed for Visual Studio compilers
target_compile_definitions(main PRIVATE -D_USE_MATH_DEFINES)

The sample project can be downloaded from here

Printable answered 20/12, 2020 at 21:39 Comment(5)
Could you help to upload a working example Clion project to github? Whatever vcpkg library is fine.Chamber
Unfortunately I don't have a CLion installed, so I cannot help with this. But the reason is in the CMakeLists.txt not in the CLion project I think.Printable
@Justalearner Just installed CLion and checked the steps. The issue seems to be most likely related to a missing OpenMesh vcpkg triplet. Will upload project tomorrow.Printable
Thank you for the demo project! @PrintableChamber
+1 for the fix, and a note to CLion users: by default CLion uses Visual Studio as a Toolchain, but the "Architecture" dropdown menu is empty. If it's empty, it'll default to x86 architecture, so be sure to explicitly choose x64 if you're working on an x64 project!Hebrides

© 2022 - 2024 — McMap. All rights reserved.