简体   繁体   English

使用 CMake find_package() 时无法打开源文件“GL/glew.h”

[英]Cannot open source file “GL/glew.h” when using CMake find_package()

as stated in the title, I am trying to add glew.h to my project built with CMake on Visual Studio 2019 (Windows 10), and in doing so am experiencing an error thrown during the building stage fatal error C1083: Cannot open include file: 'GL/glew.h': No such file or directory .如标题所述,我试图将glew.h添加到我在 Visual Studio 2019 (Windows 10) 上使用 CMake 构建的项目中,这样做时遇到了在构建阶段引发的错误fatal error C1083: Cannot open include file: 'GL/glew.h': No such file or directory

I am including it in engine.hpp as such #include <GL/glew.h>我将它包含在engine.hpp ,例如#include <GL/glew.h>

CMakeLists.txt CMakeLists.txt

cmake_minimum_required (VERSION 3.8)

set(PROJECT GUI_Engine)

add_executable (${PROJECT} "./src/driver/main.cpp")

set(HEADER_FILES ./src/include)
set(DEC_FILES ./src/lib)
set(PKGPATH C:/dev/vcpkg/packages)

add_library(engine ${DEC_FILES}/engine.cpp ${HEADER_FILES}/engine.hpp)

list(APPEND CMAKE_PREFIX_PATH ${PKGPATH}/sdl2_x64-windows)
find_package(SDL2 CONFIG REQUIRED)
target_link_libraries(${PROJECT} PRIVATE engine SDL2::SDL2main)
target_link_libraries(engine PRIVATE SDL2::SDL2)

list(APPEND CMAKE_PREFIX_PATH ${PKGPATH}/glew_x64-windows)
find_package(GLEW REQUIRED)
target_link_libraries(engine PRIVATE GLEW::GLEW)

Any help would be appreciated, and I apologize for my inexperience, thanks!任何帮助将不胜感激,我为我的经验不足表示歉意,谢谢!

If you use vcpkg, you should just use vcpkg's CMake integration and let it handle details.如果您使用 vcpkg,您应该只使用 vcpkg 的 CMake 集成并让它处理细节。 Your CMakeLists.txt should not contain any absolute path, it's supposed to be a file you share with other people alongside your source.您的 CMakeLists.txt 不应包含任何绝对路径,它应该是您与源文件一起与其他人共享的文件。

That being said, the immediate issue at hand comes probably from this problem:话虽如此,眼前的问题可能来自这个问题:

  1. target_link_libraries(${PROJECT} PRIVATE engine SDL2::SDL2main)

    From which I guess your executable #include s some header from engine at some point.我猜你的可执行文件#include在某个时候来自engine的一些头文件。 Probably engine.hpp , right?可能是engine.hpp ,对吧?

  2. target_link_libraries(engine PRIVATE GLEW::GLEW)

    Makes Glew available to engine but privately.使 Glew 可用于engine但不公开。 That is: only when building engine itself, not when building anything else (in particular, not when building the main executable).也就是说:仅在构建引擎本身时,而不是在构建其他任何东西时(特别是在构建主可执行文件时)。

From point 1., the dependency on Glew is exposed in the API of engine , so it is a public dependency.从第 1 点来看,对 Glew 的依赖暴露在engine的 API 中,所以它是一个公共依赖。 So you should mark it as PUBLIC in 2.:所以你应该在 2. 中将其标记为PUBLIC

target_link_libraries(engine PUBLIC GLEW::GLEW)

Once CMake knows the dependency is exposed, it will also make Glew available to anything that uses engine .一旦 CMake 知道依赖项被暴露,它也会使 Glew 可用于任何使用engine东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM