GLEW无法使用CMAKE

时间:2016-02-03 15:00:49

标签: c++ opengl glfw glew

我正在尝试使用GLFW和GLEW编写一个简单的程序,虽然我可以成功添加所需的GLFW库,但我不能对GLEW库执行相同的操作。 我对CMAKE完全不熟悉,所以我不知道我应该做些什么。我正在使用CLION btw。提前谢谢!

cmake_minimum_required(VERSION 3.3)
project(OpenGLHelloWorld)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)
add_executable(OpenGLHelloWorld ${SOURCE_FILES})

add_subdirectory(../glfw-3.1.2 ${CMAKE_CURRENT_BINARY_DIR}/glfw_bin)
include_directories(../glfw-3.1.2/include)
target_link_libraries(OpenGLHelloWorld glfw ${GLFW_LIBRARIES})

ADD_DEFINITIONS(-DGLEW_STATIC)
include_directories(../glew-1.13.0/include)
link_libraries(../glew-1.13.0/lib)

和cpp文件:

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdio.h>

using namespace std;

int main() {
    if(!glewInit()) {
        fprintf(stderr, "Could not start GLFW3\n");
    }

    return 0;
}

错误:

undefined reference to `glewInit@0'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\OpenGLHelloWorld.dir\build.make:97: recipe for target 'OpenGLHelloWorld.exe' failed
mingw32-make.exe[3]: *** [OpenGLHelloWorld.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/OpenGLHelloWorld.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/OpenGLHelloWorld.dir/rule] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/OpenGLHelloWorld.dir/all' failed
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/OpenGLHelloWorld.dir/rule' failed
Makefile:161: recipe for target 'OpenGLHelloWorld' failed
mingw32-make.exe: *** [OpenGLHelloWorld] Error 2

1 个答案:

答案 0 :(得分:0)

您没有将已编译的代码链接到glew。

target_link_libraries(OpenGLHelloWorld glfw ${GLFW_LIBRARIES} GLEW)

这应该修复glew lib链接到你的可执行文件。

HTH。