Linker Error During OpenGL: SuperBible Tutorial

2019-06-27 08:50发布

问题:

I'm currently trying to decide between DirectX and OpenGL by programming a little DirectX 10 and OpenGL 3.3. I already have the setup for DirectX finished, it was fairly easy to link and compile. OpenGl is... harder.

The OpenGL Superbible has a beginning example called Triangle.cpp in which we link two libraries freeglut_static.lib and GLTools.lib. This isn't a problem; I have also gone to the Project Directories and included the Include/ and lib/ path of all necessary OpenGL extensions (GLEE, Glew, Glut, FreeGlut, GLTools- damn, is that enough?).

First I had several linker errors, because my code generation was set on DLL, not Static. I have fixed this and also added LIBC.lib to the list of ignored libraries in the Linker (not sure if setting the code generation to static fixed this as well).

Now I still have two linker errors remaining which I cannot get rid of:

1>Triangle.obj : error LNK2019: unresolved external symbol ___glutInitWithExit referenced in function _glutInit_ATEXIT_HACK
1>Triangle.obj : error LNK2019: unresolved external symbol ___glutCreateWindowWithExit referenced in function _glutCreateWindow_ATEXIT_HACK

I searched this problem on google, and many people commented on the static nature of the program (which I have fixed) as well as a particular problem with a conflicting version between Glut.h and Glut.lib. However, I even used an older version of Glut (3.6) and the linker error still remains.

Other searches in google don't really come up with anything reasonable to work with. So, I'm asking here: How do I fix this?

Info

Code Generation: Multithreaded

C++ Preprocessor Command: FREEGLUT_STATIC

IDE: Visual Studio 2008 and 2010. (Testing on both- same error on both)

Ignored Libraries: LIBC.lib

Triangle.cpp code (a simple copy/paste from the code in the book):

// Triangle.cpp
// Our first OpenGL program that will just draw a triangle on the screen.
#include <GLTools.h> // OpenGL toolkit
#include <GLShaderManager.h> // Shader Manager Class

//#define FREEGLUT_STATIC

#include <GL/glut.h> // Windows FreeGlut equivalent

GLBatch triangleBatch;
GLShaderManager shaderManager;
///////////////////////////////////////////////////////////////////////////////
// Window has changed size, or has just been created. In either case, we need
// to use the window dimensions to set the viewport and the projection matrix.
void ChangeSize(int w, int h)
{
    glViewport(0, 0, w, h);
}
///////////////////////////////////////////////////////////////////////////////
// This function does any needed initialization on the rendering context.
// This is the first opportunity to do any OpenGL related tasks.
void SetupRC()
{
    // Blue background
    glClearColor(0.0f, 0.0f, 1.0f, 1.0f );
    shaderManager.InitializeStockShaders();
    // Load up a triangle
    GLfloat vVerts[] = { -0.5f, 0.0f, 0.0f,
        0.5f, 0.0f, 0.0f,
        0.0f, 0.5f, 0.0f };
    triangleBatch.Begin(GL_TRIANGLES, 3);
    triangleBatch.CopyVertexData3f(vVerts);
    triangleBatch.End();
}
///////////////////////////////////////////////////////////////////////////////
// Called to draw scene
void RenderScene(void)
{
    // Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
    shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vRed);
    triangleBatch.Draw();
    // Perform the buffer swap to display the back buffer
    glutSwapBuffers();
}
///////////////////////////////////////////////////////////////////////////////
// Main entry point for GLUT based programs
int main(int argc, char* argv[])
{
    gltSetWorkingDirectory(argv[0]);
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
    glutInitWindowSize(800, 600);
    glutCreateWindow("Triangle");
    glutReshapeFunc(ChangeSize);
    glutDisplayFunc(RenderScene);
    GLenum err = glewInit();
    if (GLEW_OK != err) {
        fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
        return 1;
    }
    SetupRC();
    glutMainLoop();
    return 0;
}

回答1:

Alright, here is the answer:

I had to add the following commands:

#pragma (lib, "freeglut_static.lib")
#pragma (lib, "gltools.lib")

after putting in the library path of the needed files in the Project Directories. After that I also needed to include glut32.dll in my Solution directory (as I had also linked to Glut32), and then it worked.

Hope this helps someone!



回答2:

For future reference you need to supply the names of the libraries you want to link with at the linker stage.

In visual studio for example you'll find this under

Project Properties->Linker->Input

Just add them ';' delimited in the "Additional Dependencies" field.

Personally I like the #pragma comment solution though as you don't need to remember to add the .libs under additional dependencies you just need those #pragma statements somewhere.