Search code examples
cmakevcpkgcmakelists-options

Including libraries from vcpkg only


I want to install and use libraries only from vcpkg. Currently, i placed my vcpkg installation in my project, so i think i can just carry the project wherever i want and it will work automatically as npm install but without running a command.

I code in vscode on Ubuntu. I am using assimp, imgui, opengl, glfw3 and glad libraries.

i don't know how i can include those libraries from vcpkg. I am so confused about the methods i saw when researching.

Some people use find_package(OpenGL REQUIRED), or find_library(OPENGL_LIBRARY OpenGL or GL ). Sometimes they use add_library().

Sometimes they use target_include_directories() alongside these above or just include_directories().

I don't understand what the pipeline is for this instructions. The only thing i understood is that i will always use target_link_libraries() to my target after pull all these libraries somehow.

Everyone copy and pastes same tutorial information everywhere without giving any specific detail.

For example for installing OpenGL everyone says find_package(OpenGL REQUIRED). But this tries to find the module lopenGL as if it is in the system directory.

I am not even talking the ones throws things like set cmake_toolchain etc.

I really need an explanation for these issues.

I tried all these above to make it work but i end up like i am throwing something to the wall and see what sticks. Which is not a method i like.


Solution

  • What you need to do is first set the CMAKE_TOOLCHAIN_FILE see the documentation here.

    If you are using VSCode (with CMakeTools for VSCode) then you can change the settings.json by adding this:

    {
      "cmake.configureSettings": {
        "CMAKE_TOOLCHAIN_FILE": "path/to/vcpkg_root_folder/scripts/buildsystems/vcpkg.cmake"
      }
    }
    

    Or you can pass it to cmake directly via a (define) -D flag:

    cmake -S. -Bbuild -DCMAKE_TOOLCHAIN_FILE=/same/path/as/above
    

    By specifing the toolchain file you can then freely use find_package(PACKAGE) to get predefined variables for linking, include folders, etc... The toolchainfile somewhat guarantees that the first libraries CMake will find and use are the ones downloaded/installed by vcpkg.

    An example of such predefined variables for find_package(OpenGL) can be found here

    EDIT: If you are confused by the find_package then to briefly explain:

    Think of it as just a function that loads a specific .cmake file. This file contains (or creates) said predefined variables and check that these files exist - the behaviour of the file that it loads usually depends on what kind of a file it loads (either *config.cmake or find*.cmake). As the naming suggests, find*.cmake most of the time contains logic on where to look for the needed files, while *config.cmake already knows of the existence of said files and just configures the variables. Without these variables you would have to manually look for the files and define the paths yourself. e.g.

    g++ some_file.cpp -Lpath/to/library/folder -lname_of_library ...
    

    If one framework contains multiple libraries (like openCV) you would have to type out all the libraries by hand. But with a predefined variable you could just:

    target_link_libraries(target ${OPENCV_LIBRARIES}) and be done with it.