Search code examples
c++visual-studiocmakeglfwvcpkg

GLFW directory not found with CMake and vcpkg


I have been unable to figure out how to get CMake to find and set correct GLFW CMake constants when using CMake in VS2017. Any help will be greatly appreciated :).

I downloaded glfw3 through Microsoft's vcpkg tool. I have checked that files do physically exist in the directory that vcpkg puts them in (~\vcpkg\installed\x86-windows\include). I set up my CMakeSettings.json as per their docs here. I used that tutorial as a basis for getting GLFW to be set up correctly.

I then use find_package(glfw3 REQUIRED) to find the glfw3 library. This does not spit out any errors. Actually the CMakeLists.txt doesn't complain at all. It's at the compile stage where it complains.

After that I add glfw3 with target_link_libraries(exe ${GLFW3_LIBRARIES}) to the executable.

Then when I try and build a simple example (including the header file), the compilation fails because it cannot find GLFW/glfw3.h.

The error from MSVC:

fatal error C1083: Cannot open include file: 'GLFW/glfw3.h': No such file or directory 

Here is my CMakeLists.txt for added reference:

cmake_minimum_required(VERSION 3.7)
project(learn-opengl)
find_package(glfw3 REQUIRED)

add_executable(learn-opengl main.cpp)
target_link_libraries(learn-opengl ${GLFW3_LIBRARIES})

GLFW3_LIBRARIES I got from glfw3Config.cmake by snooping around what vcpkg puts in the installed directory (~\vcpkg\installed\x86-windows\share\glfw3)

And just in case, the main.cpp:

#include <GLFW/glfw3.h>

int main()
{
    return 0;
}

I have tried calling cmake from the command line as well, but to no avail that didn't work either.

Am I missing something? Did I perhaps misunderstand something in vcpkg documentation? I have really no idea what I am missing... :/ I should say, in addition, that I am fairly new to CMake as well.


Solution

  • Reformulating my previous comment as answer:

    You should add the imported target glfw to your target_link_libraries command instead of ${GLFW3_LIBRARIES}. The find_package(glfw3) generates an import target glfw. By making your target learn-opengl dependent on this imported target you specify both the library to link with and the include directories to use.