我正在尝试为我的项目构建一个CMakeLists.txt
。它需要包括SDL,OpenGL和GLEW以及我所有的* .cpp和* .h文件在不同的目录中。 用于构建项目的所有文件都在调用cmake的项目源目录中。
错误:
In file included from /home/me/repos/AlchemyEngine/alchemy/alcEngine.cpp:1:0:
/home/me/repos/AlchemyEngine/alchemy/alcEngine.h:69:18: fatal error: glew.h: No such file or directory
#include <glew.h>
我认为最简单的方法是在Visual Studio中显示设置此项目的步骤(该方法可以像我以前从那里开发一样工作),希望我能在CMakeLists.txt文件中重新创建设置时得到帮助。 / p>
在Visual Studio中设置项目
1. Includes
Project -> *ProjectName* Properties -> VC++ Directories
Click Include Directories -> Click down-arrow- > edit
Browse for the include files (...\SDL2-OpenGL-GLEW-VC++\include)
2. Libraries
Project -> *ProjectName* Properties -> VC++ Directories
Click Library Directories -> Click down-arrow -> edit
Browse for the library files (...\SDL2-OpenGL-GLEW-VC++\lib)
3. Linkers
Project -> *ProjectName* Properties -> Linker -> Input
Click Additional Dependencies -> Click down arrow -> edit
Copy into box:
SDL2.lib
SDL2main.lib
SDL2_image.lib
OpenGL32.lib
glu32.lib
glew32.lib
4. DLLs
Build project with chosen entry point to create Debug folder with executable.
Copy the .dll files from the ...\SDL-OpenGL-GLEW-VC++\dropInExe folder and paste into the
Debug folder with the project executable
(...\*Project Folder*\Debug)
这是我的CMakeLists.txt
文件
cmake_minimum_required (VERSION 3.5)
project (AlchemyEngine)
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
include_directories (
alchemy/
alchemy/event/
alchemy/scene/
alchemy/resource/
alchemy/render/
alchemy/util/
game/
#SDL2_OpenGL_GLEW_VC++/include
)
set( srcs
alchemy/alcEngine.cpp
alchemy/event/alcEvent.cpp
alchemy/render/alcRenderer.cpp
alchemy/render/alcSprite.cpp
alchemy/resource/alcResource.cpp
alchemy/resource/alcShader.cpp
alchemy/resource/alcTexture.cpp
alchemy/scene/alcObject.cpp
alchemy/scene/alcScene.cpp
alchemy/util/alcBlockAllocator.cpp
alchemy/util/alcStackAllocator.cpp
game/Player.cpp
Main.cpp
)
add_library (libraries STATIC SDL2_OpenGL_GLEW_VC++/include/)
add_executable (alc ${PROJECT_SOURCE_DIR}/${srcs})
target_link_libraries(alc libraries)
set_target_properties(libraries PROPERTIES LINKER_LANGUAGE CXX)
set_target_properties(alc PROPERTIES LINKER_LANGUAGE CXX)
配置和生成后,$CMAKE_SOURCE_DIR/build/bin
包含:
liblibraries.a
glew32.dll
libpng16-16.dll
SDL2.dll
SDL2_image.dll
zlib1.dll
在我看来,我包含头文件的方法是不正确的,但是在CMake文档和其他堆栈交换中打乱信息并没有产生更好的结果。我感觉它与链接器步骤有关,但是我无法弄清楚如何在CMake中指定依赖项。
如果项目结构对于此问题如此重要,我也可以tree
并发布输出。
最终编辑:
该错误已在以下答案中得到修复,这是我对任何感兴趣的人的最终CMakeLists.txt
# Outputs executable(s) in {PROJECT_SOURCE_DIR}/build/bin/
# specify min version and project name
cmake_minimum_required (VERSION 3.5)
project (AlchemyEngine)
# set output paths for binaries/executable
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
# include project header files
include_directories (
alchemy/
alchemy/event/
alchemy/scene/
alchemy/resource/
alchemy/render/
alchemy/util/
game/
)
# create source files definition
set( srcs
alchemy/alcEngine.cpp
alchemy/event/alcEvent.cpp
alchemy/render/alcRenderer.cpp
alchemy/render/alcSprite.cpp
alchemy/resource/alcResource.cpp
alchemy/resource/alcShader.cpp
alchemy/resource/alcTexture.cpp
alchemy/scene/alcObject.cpp
alchemy/scene/alcScene.cpp
alchemy/util/alcBlockAllocator.cpp
alchemy/util/alcStackAllocator.cpp
game/Player.cpp
Main.cpp
)
# add a library target to be built from the source files
add_library (libraries STATIC SDL2_OpenGL_GLEW_VC++/lib/)
# specify include directories
target_include_directories(libraries PUBLIC SDL2_OpenGL_GLEW_VC++/include/)
# specify libraries to use when linking the library
target_link_libraries (libraries
${PROJECT_SOURCE_DIR}/SDL2_OpenGL_GLEW_VC++/lib/glew32.lib
${PROJECT_SOURCE_DIR}/SDL2_OpenGL_GLEW_VC++/lib/glew32s.lib
${PROJECT_SOURCE_DIR}/SDL2_OpenGL_GLEW_VC++/lib/SDL2_image.lib
${PROJECT_SOURCE_DIR}/SDL2_OpenGL_GLEW_VC++/lib/SDL2.lib
${PROJECT_SOURCE_DIR}/SDL2_OpenGL_GLEW_VC++/lib/SDL2main.lib
)
# create the executable using the source files defined above
add_executable (alc ${PROJECT_SOURCE_DIR}/${srcs})
# specify the headers
target_include_directories(alc PUBLIC ${PROJECT_SOURCE_DIR})
# link the library to the project
target_link_libraries(alc libraries)
# force the linker language because there's some c stuff in the library files
set_target_properties(libraries PROPERTIES LINKER_LANGUAGE CXX)
set_target_properties(alc PROPERTIES LINKER_LANGUAGE CXX)
答案 0 :(得分:2)
target_include_directories(libraries PRIVATE SDL2_OpenGL_GLEW_VC++/include/)
应该使用PUBLIC
,以便与libraries
链接的任何可执行目标(例如target_link_libraries(alc libraries)
)都知道此路径。
这是假设glew.h
位于SDL2_OpenGL_GLEW_VC++/include/
目录中。
由于target_include_directories
使用的是相对路径(我很确定它是相对于源树的),因此需要确保此路径对您的目录树是正确的。