SpartanJ / SOIL2

SOIL2 is a tiny C library used primarily for uploading textures into OpenGL.
MIT No Attribution
370 stars 75 forks source link

Undefined reference while linking in Windows #14

Closed SpartanJ closed 8 years ago

SpartanJ commented 8 years ago

Original report by Vadim Melnicuk (Bitbucket: vadimmelnicuk, GitHub: vadimmelnicuk).


I am getting the list of following errors while linking static library on Windows using CMake:

#!c

C:/.../lib/windows/libsoil2.a(SOIL2.o):SOIL2.c:(.text+0x10a0b): undefined reference to `wglGetProcAddress@4'
C:/.../lib/windows/libsoil2.a(SOIL2.o):SOIL2.c:(.text+0x10ab0): undefined reference to `glGetIntegerv@8'
C:/.../lib/windows/libsoil2.a(SOIL2.o):SOIL2.c:(.text+0x10f9b): undefined reference to `glReadPixels@28'
C:/.../lib/windows/libsoil2.a(SOIL2.o):SOIL2.c:(.text+0x11cd7): undefined reference to `glGetError@0'
C:/.../lib/windows/libsoil2.a(SOIL2.o):SOIL2.c:(.text+0x11e90): undefined reference to `glGetIntegerv@8'
C:/.../lib/windows/libsoil2.a(SOIL2.o):SOIL2.c:(.text+0x11eb2): undefined reference to `glPixelStorei@8'
C:/.../lib/windows/libsoil2.a(SOIL2.o):SOIL2.c:(.text+0x11fc5): undefined reference to `glGetError@0'
C:/.../lib/windows/libsoil2.a(SOIL2.o):SOIL2.c:(.text+0x1204f): undefined reference to `glGetError@0'
C:/.../lib/windows/libsoil2.a(SOIL2.o):SOIL2.c:(.text+0x12118): undefined reference to `glPixelStorei@8'
C:/.../lib/windows/libsoil2.a(SOIL2.o):SOIL2.c:(.text+0x12228): undefined reference to `glPixelStorei@8'
C:/.../lib/windows/libsoil2.a(SOIL2.o):SOIL2.c:(.text+0x12506): undefined reference to `glGetIntegerv@8'
C:/.../lib/windows/libsoil2.a(SOIL2.o):SOIL2.c:(.text+0x1280f): undefined reference to `glGetIntegerv@8'
C:/.../lib/windows/libsoil2.a(SOIL2.o):SOIL2.c:(.text+0x1282d): undefined reference to `glPixelStorei@8'
C:/.../lib/windows/libsoil2.a(SOIL2.o):SOIL2.c:(.text+0x12a8d): undefined reference to `glPixelStorei@8'
C:/.../lib/windows/libsoil2.a(SOIL2.o):SOIL2.c:(.text+0x12c93): undefined reference to `glGetIntegerv@8'
C:/.../lib/windows/libsoil2.a(SOIL2.o):SOIL2.c:(.text+0x12cb1): undefined reference to `glPixelStorei@8'
C:/.../lib/windows/libsoil2.a(SOIL2.o):SOIL2.c:(.text+0x1311d): undefined reference to `glGetIntegerv@8'
C:/.../lib/windows/libsoil2.a(SOIL2.o):SOIL2.c:(.text+0x14556): undefined reference to `glGetError@0'
C:/.../lib/windows/libsoil2.a(SOIL2.o):SOIL2.c:(.text+0x14590): undefined reference to `glGetIntegerv@8'
C:/.../lib/windows/libsoil2.a(SOIL2.o):SOIL2.c:(.text+0x145ae): undefined reference to `glPixelStorei@8'
C:/.../lib/windows/libsoil2.a(SOIL2.o):SOIL2.c:(.text+0x14621): undefined reference to `glGetError@0'
C:/.../lib/windows/libsoil2.a(SOIL2.o):SOIL2.c:(.text+0x14642): undefined reference to `glPixelStorei@8'
C:/.../lib/windows/libsoil2.a(SOIL2.o):SOIL2.c:(.text+0x14763): undefined reference to `glPixelStorei@8'
SpartanJ commented 8 years ago

Original comment by Martín Lucas Golini (Bitbucket: SpartanJ, GitHub: SpartanJ).


You must link against OpenGL ( in mingw should be "opengl32" lib ).

SpartanJ commented 8 years ago

Original comment by Vadim Melnicuk (Bitbucket: vadimmelnicuk, GitHub: vadimmelnicuk).


I have linked opengl32 using find_package(OpenGL REQUIRED) and target_link_libraries(${OPENGL_gl_LIBRARY}) later. I am also including <GL/glew.h> before . Neither glfw nor glew complain about opengl references.

SpartanJ commented 8 years ago

Original comment by Martín Lucas Golini (Bitbucket: SpartanJ, GitHub: SpartanJ).


I don't use CMake, but i did a quick test and builded without problems.

I'm pretty sure that your mistake is here:

target_link_libraries(${OPENGL_gl_LIBRARY})

it should be:

target_link_libraries(binaryname ${OPENGL_LIBRARIES})

You can see the CMakeLists file that i created, here: http://pastebin.com/aVvrt6Nx ( if you wan't to use it you'll need to get the FindSDL2.cmake file from here: https://github.com/tcbrindle/sdl2-cmake-scripts ( and place it in the subfolder cmake ). And used mingw to build it ( cmake -G "MinGW Makefiles" CMakeLists.txt ).

SpartanJ commented 8 years ago

Original comment by Vadim Melnicuk (Bitbucket: vadimmelnicuk, GitHub: vadimmelnicuk).


I am trying to link against SOIL2 static library, that is libsoil2.a I have also changed OPENGL_gl_LIBRARY to OPENGL_LIBRARIES, but soil still fails to find a reference to opengl functions.

SpartanJ commented 8 years ago

Original comment by Martín Lucas Golini (Bitbucket: SpartanJ, GitHub: SpartanJ).


If you want to, you can show me the CMakeList.txt that you are using. But since i don't even know more than the basics of CMake and i don't provide support for CMake with the library, i think that it would be more appropriate to ask how to solve your issue in a CMake forum, since it's not a problem with SOIL but with your Makefile generation. Anyways, show me your CMake file at least to see if i can help.

SpartanJ commented 8 years ago

Original comment by Vadim Melnicuk (Bitbucket: vadimmelnicuk, GitHub: vadimmelnicuk).


Hello, Martin. Sorry for the late response. I have included my CMakeLists.txt below: Also, this links well on linux and osx, windows complains about gl references though.

#!c
cmake_minimum_required(VERSION 3.6)
project(Neoconomy)

if (WIN32)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
else ()
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif (WIN32)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/")

set(SOURCE_FILES main.cpp HUGE_LIST_OF_FILES)
add_executable (${PROJECT_NAME} ${SOURCE_FILES})

find_package(OpenGL REQUIRED)
find_package(glew REQUIRED)
find_package(glfw3 3.2 REQUIRED)
find_package(soil2 REQUIRED)

if (WIN32)
    target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES} ${GLFW_LIBRARIES} ${GLEW_LIBRARIES} ${SOIL2_LIBRARIES})
else ()
    if (APPLE)
        target_link_libraries(${PROJECT_NAME} "-framework CoreFoundation" ${OPENGL_LIBRARIES} ${GLFW_LIBRARIES} ${GLEW_LIBRARIES} ${SOIL2_LIBRARIES})
    else ()
        target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES} ${GLFW_LIBRARIES} ${GLEW_LIBRARIES} ${SOIL2_LIBRARIES})
    endif (APPLE)
endif (WIN32)

include_directories(${CMAKE_SOURCE_DIR}/include) # Header only libraries are found here
include_directories(${OPENGL_INCLUDE_DIR})
include_directories(${GLEW_INCLUDE_DIR}) # GLEW INCLUDE DIR DOI!
include_directories(${GLFW_INCLUDE_DIR}) # GLFW INCLUDE DIR DOI!
include_directories(${SOIL2_INCLUDE_DIR})
SpartanJ commented 7 years ago

Original comment by Rubick (Bitbucket: CuriousRubick, GitHub: CuriousRubick).


I fixed this problem by simply linking SOIL2 before opengl32.lib . If using Codeblocks, go to build options -> linker settings, and move SOIL2 above opengl.

Wasn't an issue in Visual Studio.