我正在尝试将一个OpenGl项目用于CLion。我正在使用3个不同的库:GLEW,GLFW和SOIL。
包含似乎工作正常,一切都找到了,但每次我尝试构建时都会出错:
undefined reference to `glfwInit'
undefined reference to `glfwWindowHint'
undefined reference to `glfwWindowHint'
undefined reference to `glfwWindowHint'
undefined reference to `glfwWindowHint'
undefined reference to `glfwCreateWindow'
...
C:/Users/John/OneDrive/OpenGL Projects/OpenGL/Lib_files/SOIL/lib/libSOIL.a(SOIL.o):SOIL.c:(.text+0x3e): undefined reference to `glGetString@4'
C:/Users/John/OneDrive/OpenGL Projects/OpenGL/Lib_files/SOIL/lib/libSOIL.a(SOIL.o):SOIL.c:(.text+0x72): undefined reference to `glGetString@4'
...
这是我在cmake文件中得到的内容:
cmake_minimum_required(VERSION 3.6)
project(OpenGL)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp Shader.h Shader.cpp)
set(LIBS_DIR C:/Users/John/OneDrive/OpenGL\ Projects/OpenGL/Lib_files)
set(GLEW_ROOT_DIR ${LIBS_DIR}/GLEW )
set(GLFW_ROOT_DIR ${LIBS_DIR}/GLFW )
set(SOIL_ROOT_DIR ${LIBS_DIR}/SOIL )
set(GLEW_INCLUDE_DIRS ${GLEW_ROOT_DIR}/include)
set(GLFW_INCLUDE_DIRS ${GLFW_ROOT_DIR}/include)
set(SOIL_INCLUDE_DIRS ${SOIL_ROOT_DIR}/include)
set(GLEW_LIBRARY ${GLEW_ROOT_DIR}/lib/libglew32.a)
set(GLUT_LIBRARY ${GLFW_ROOT_DIR}/lib/libglfw3.a)
set(SOIL_LIBRARY ${SOIL_ROOT_DIR}/lib/libSOIL.a)
include_directories( ${GLEW_INCLUDE_DIRS} ${GLFW_INCLUDE_DIRS} ${SOIL_INCLUDE_DIRS})
add_executable(OpenGL ${SOURCE_FILES})
target_link_libraries(OpenGL libopengl32.a ${GLEW_LIBRARY} ${GLFW_LIBRARY} ${SOIL_LIBRARY})
一旦我尝试构建,我就会为GLFW和SOIL带来“未定义引用”错误,但不会为GLEW错误。
我做错了什么?
答案 0 :(得分:1)
我也遇到了这个问题,添加" glfw"在target_link_libraries()的末尾可能会有所帮助。我从reddit得到了这个解决方案。 https://www.reddit.com/r/GraphicsProgramming/comments/76rtt5/linking_glfw3_with_clion/
答案 1 :(得分:0)
您的CMake文件中存在多个问题。第一:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
要请求C ++ 11,您可能更喜欢在目标上设置CXX_STANDARD
,或者如果要在全局范围内设置CMAKE_CXX_STANDARD
。 CMAKE_CXX_STANDARD_REQUIRED
可用于使您想要的版本成为一项要求:
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
您不应该调用可执行文件OpenGL
,因为它与库不明确。
要链接库,您应该依靠find_package
为每个库设置变量:
find_package(GLEW REQUIRED)
请注意,您没有链接到OpenGL,因此未定义对glGetString
的引用。对于其他未定义的引用,我猜你的变量设置错误。
每个包的正确配置都不同,但这是项目的清理版本:
cmake_minimum_required(VERSION 3.6)
project(MyOpenGLProject)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(GLFW REQUIRED)
find_package(SOIL REQUIRED)
set(SOURCE_FILES main.cpp Shader.h Shader.cpp)
add_executable(my_opengl_executable ${SOURCE_FILES})
target_link_libraries(my_opengl_executable ${OPENGL_LIBRARIES} GLEW::GLEW ${GLFW_LIBRARY} ${SOIL_LIBRARY})
target_include_directories(my_opengl_executable PUBLIC ${OPENGL_INCLUDE_DIR} ${GLFW_INCLUDE_DIR} ${SOIL_INCLUDE_DIR})
set_target_properties(my_opengl_executable PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED ON
)
请注意,GLEW声明了imported target GLEW::GLEW
,它嵌入了包含目录和其他参数:您只需要使用taget_link_libraries
链接它。
您需要为find_package
设置GLFW和SOIL的提示变量才能生效。当您尝试配置上面的已清理版本时,应在错误日志中输出变量名称。