Unresolved external symbol __imp_glX

How to fix these errors?
unresolved external symbol __imp_glBindBuffer referenced in function main
unresolved external symbol __imp_glGenBuffers referenced in function main
unresolved external symbol __imp_glBufferData referenced in function main
unresolved external symbol __imp_glAttachShader referenced in function main

My CMakeLists.txt looks like this

cmake_minimum_required(VERSION 3.7.1)
...
set(SDL2_LIB ${THIRDPARTY_DIR}/SDL2/bin/SDL2.lib)
file(COPY
    ${THIRDPARTY_DIR}/SDL2/bin/SDL2.dll
    DESTINATION ${BIN_DIR})
find_package(OpenGL) # <-- I use this instead of adding the .lib file.
...
target_link_libraries(
    ${PROJECT_NAME}
    LINK_PUBLIC
    ${SDL2_LIB}
    ${OPENGL_LIBRARIES}
    ${VULKAN_LIB_LIST})

The idea about find_package() and ${OPENGL_LIBRARIES} is from this https://github.com/knightcrawler25/GLSL-PathTracer/blob/master/CMakeLists.txt
I notice that OPENGL_LIBRARIES is not set in the CMakeLists.txt.

I googled my problem and people say to add ‘opengl32.lib’ to the linker input, but I don’t understand it, I tried this which doesn’t work:

target_link_libraries(
    ${PROJECT_NAME}
    LINK_PUBLIC
    ${SDL2_LIB}
    #${OPENGL_LIBRARIES}
    opengl32.lib # <-- I added this but it doesn't work.
    ${VULKAN_LIB_LIST})

According to the example in the link I showed (go to the GitHub parent directory > thirdparty/gl3w/GL), they don’t add the file "opengl32.lib" which is not in the project library too and which is good, smallest project as possible. But I don’t know how to do that.

I used this OpenGL example where there’s an exact comment with the same problem in the comment section

Quote from somewhere:

Type cmake --help-module findopengl from a command prompt with cmake in the path. That will tell you the targets available to add to target_link_libraries()

The result:
[…]
IMPORTED Targets

This module defines the IMPORTED targets:

OpenGL::GL
Defined to the platform-specific OpenGL libraries if the system has OpenGL.
OpenGL::GLU
Defined if the system has OpenGL Utility Library (GLU).

OpenGL::OpenGL
Defined to libOpenGL if the system is GLVND-based.
OpenGL::GLX
Defined if the system has OpenGL Extension to the X Window System (GLX).
OpenGL::EGL
Defined if the system has EGL.
[…]

Can someone explain how to change the line target_link_libraries() to fix the problem? Because I tried target_link_libraries(${PROJECT_NAME} ... OpenGL::OpenGL) but it’s wrong because OpenGL::OpenGL.lib is wrong.

I’m no CMake guru, but I think you want something like this:

# Find packages of additional libraries
#   - OpenGL
find_package( OpenGL REQUIRED )
if ( OPENGL_FOUND )
    include_directories( ${OPENGL_INCLUDE_DIRS} )
    link_libraries     ( ${OPENGL_LIBRARIES}    )
endif()

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.