简体   繁体   中英

GLEW won't work with CMAKE

I'm trying to write a simple program with GLFW and GLEW, and while I could successfully add the needed GLFW libraries I can't do the same with the GLEW ones. I'm totally new to CMAKE so I don't know what I should do differently. I am using CLion btw. Thanks in advance!

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)

And the cpp file:

#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;
}

And the error:

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

You didn't link your compiled code to glew.

target_link_libraries(OpenGLHelloWorld glfw ${GLFW_LIBRARIES} GLEW)

That should fix glew lib linking to your executable.

HTH.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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