Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use a library installed with VCPKG in CLion

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.
like image 236
FourZeroFive Avatar asked Mar 14 '20 15:03

FourZeroFive


People also ask

How do I use CMake with Vcpkg?

CMake (Toolchain File) The best way to use installed libraries with cmake is via the toolchain file scripts\buildsystems\vcpkg. cmake . To use this file, you simply need to add it onto your CMake command line as: -DCMAKE_TOOLCHAIN_FILE=D:\src\vcpkg\scripts\buildsystems\vcpkg.

Should I add Vcpkg to path?

It is recommended to clone vcpkg as a submodule for CMake projects, but to install it globally for MSBuild projects. If installing globally, we recommend a short install path like: C:\src\vcpkg or C:\dev\vcpkg, since otherwise you may run into path issues for some port build systems.


1 Answers

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

like image 66
vre Avatar answered Sep 19 '22 06:09

vre