6

For some reason my OpenGL library file isn't linking correctly for Visual Studio 2013.

Here is all of my code:

#include <gl\glew.h>
#include <GL\GL.h>
#include <GL\GLU.h>
#include <GL\freeglut.h>

#include <iostream>
#include <cstdlib>

#pragma comment( lib, "OpenGL32.lib" )
#pragma comment( lib, "glu32.lib" )
#pragma comment( lib, "freeglut.lib" )

void init( void );
void display( void );

int main( int argc, char* argv[] )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA );
    glutInitWindowSize( 512, 512 );
    glutInitContextVersion( 4, 0 );
    glutInitContextProfile( GLUT_CORE_PROFILE );
    glutCreateWindow( argv[0] );

    glutDisplayFunc( display );

    glutMainLoop();

    return EXIT_SUCCESS;
}

void init( void ) { }

void display( void )
{
    glClear( GL_COLOR_BUFFER_BIT );

    return;
}

I compiled freeglut on my own system and I don't get any errors from the program if I remove anything specifically having to do with OpenGL32.lib. (i.e. if I remove the glClear() function, my program compiles and runs with no problems.) As soon as I add any GL commands though I start getting linking errors.

1>main.obj : error LNK2019: unresolved external symbol __imp__glClear@4 referenced in function "void __cdecl display(void)" (?display@@YAXXZ)

I've also tried taking out the #pragma directive and adding the OpenGL32.lib file into the additional dependencies for the linker and I've tried calling the #pragma directives before anything else in the program to no avail. I'm running a 64x Windows 8.1 box with an AMD Radeon 7700 HD series graphics card and brand new drivers for said card. Anyone got any ideas about what may be happening?

2
  • 1
    Okay I figured it out. I had to set my build configuration to x64. I guess the .lib I have, even though it's under the Program Files(x86) directory is still a 64 bit library.
    – wmaxlees
    Mar 7, 2014 at 1:18
  • If you have the answer to your own problem, then please post it as an answer and mark it as Accepted, rather than a comment.
    – MicroVirus
    Apr 21, 2015 at 0:10

2 Answers 2

4

The problem is that linker cannot find an opengl32.lib file. Try to find it under VC2013. Make sure that the path to it is included in additional library paths. If it is not there - add it. If you didn't find the library - install Windows SDK. The 64-bit version of opengl32.lib (ignore the 32 in the library, it doesn't mean anything) should be under %ProgramFiles%\Microsoft SDKs\Windows\\Lib\x64 directory. Add this path to lib paths of your project.

4
  • Yeah I actually had the same idea so I took the OpenGL32.Lib file out of the lib folder and moved it directly into the project folder and then linked it with a #pragma comment( lib, "./OpenGL32.Lib" ) but I still get the same errors. And I reinstalled the SDK as well and it didn't seem to fix the problem.
    – wmaxlees
    Mar 7, 2014 at 0:18
  • Also, when I change the name of the library I'm trying to link to something like OpenGL32aaa.lib, I get this sort of error instead: 1>LINK : fatal error LNK1104: cannot open file './OpenGL32aaa.lib'
    – wmaxlees
    Mar 7, 2014 at 0:30
  • This means that the linker still can not find it. Don't copy the lib file. Leave it where it is. Just update the lib path.
    – vmax33
    Mar 7, 2014 at 5:42
  • Go to the Linker General property page, select Additional Library Dependencies and then specify the relative path of the file.
    – vmax33
    Mar 7, 2014 at 16:24
2

As D'artanian already mentioned, you have to link with opengl32.lib (not DLL).

I had exactly same problem and what helped was explicitly setting the library I needed to link with (Project options->Linker->Input). Setting an additional directory for the linker was not enough and you have to set path to library build for appropriate architecture. (On my system, it's a bit non-intuitive: 32b version is %ProgramFiles%\Microsoft SDKs\Windows\v7.1A\Lib\OpenGL32.Lib and 64b version is %ProgramFiles%\Microsoft SDKs\Windows\v7.1A\Lib\x64\OpenGL32.Lib.

Not the answer you're looking for? Browse other questions tagged or ask your own question.