Can't get GLEW static library to link properly on windows using cmake

35 Views Asked by At

I have a c++ project set up on windows using cmake that should link to the libaries glfw and glew for use of messing around with opengl

cmake_minimum_required(VERSION 3.28)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

project(BideoGame VERSION 1.0)

add_compile_definitions(GLEW_STATIC)

find_package(OpenGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIRS})

set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/lib/glfw-3.3.9)

file(GLOB_RECURSE SRC_FILES  src/*.cpp)
add_executable(BideoGame ${SRC_FILES})

target_link_libraries(BideoGame glfw OpenGL::GL )
target_link_libraries(BideoGame OpenGL::GL)
target_link_libraries(BideoGame ${CMAKE_CURRENT_SOURCE_DIR}/lib/glew-2.1.0/lib/Release/x64/glew32s.lib)

target_include_directories(BideoGame PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/lib/glew-2.1.0/include)

I have the binary format version of glew installed and should be linking to the glew32s.lib file here's the main.cpp

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


int main(void)
{
    GLFWwindow* window;

    if (!glfwInit())
        return -1;

    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);

    glewInit();

    while (!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT);

        glBegin(GL_TRIANGLES);
        glVertex2f(-0.5f,-0.5f);
        glVertex2f(0.5f, -0.5f);
        glVertex2f(0.0f, 0.5f);
        glEnd();

        glfwSwapBuffers(window);

        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

I'm able to compile the code if I exclude the glewInit() line but when I have the line in I get CMakeFiles/BideoGame.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x7e): undefined reference to glewInit@0'`

not sure how to fix the issue

0

There are 0 best solutions below