I just started looking at the basics of openGL, but I keep getting error LNK2019 and I don't know what's causing it or how to fix it.

#include "stdafx.h"
#include <cstdlib>
#include <GLTools.h>
#include <GLShaderManager.h>

//Include OpenGL header files, so that we can use OpenGL
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

using namespace std;

GLBatch         a;
GLShaderManager b;

//Called when a key is pressed
void handleKeypress(unsigned char key, int x, int y){
    switch(key){
        case 27: //Escape key
            exit(0);
    }
}

//Initializes 3D rendering
void initRendering(){
    glEnable(GL_DEPTH_TEST);
}

//Called when the window is resized
void handleResize(int w, int h){
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}

//Draws the 3D scene
void drawScene(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
    glLoadIdentity(); //Reset the drawing perspective

    GLfloat colour[] = {1.0f, 0.0f, 0.0f, 1.0f};
    b.UseStockShader(GLT_SHADER_IDENTITY, colour);
    a.Draw();

    glutSwapBuffers(); //Draw the scene on the screen
}
//Only called once, in main
void setupScene(){
    glClearColor(0.98f, 0.625f, 0.12f, 1.0f);
    b.InitializeStockShaders();
    GLfloat vertices[] = {0.0f, 0.0f, 0.0f,
                          0.0f, 1.0f, 0.0f,
                          1.0f, 0.0f, 0.0f};
    a.Begin(GL_TRIANGLES, 3);
    a.CopyVertexData3f(vertices);
    a.End();
}
int main(int argc, char** argv){
    //Initialize GLUT
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(400, 400);

    //Create the window
    glutCreateWindow("Window name");
    initRendering();

    //Set handler functions
    glutDisplayFunc(drawScene);
    glutKeyboardFunc(handleKeypress);
    glutReshapeFunc(handleResize);

    setupScene();

    glutMainLoop();
    return 0;
}

Errors:

1>gltools test.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall GLBatch::Draw(void)" (?Draw@GLBatch@@UAEXXZ) referenced in function "void __cdecl drawScene(void)" (?drawScene@@YAXXZ)
1>gltools test.obj : error LNK2019: unresolved external symbol "public: int __cdecl GLShaderManager::UseStockShader(enum GLT_STOCK_SHADER,...)" (?UseStockShader@GLShaderManager@@QAAHW4GLT_STOCK_SHADER@@ZZ) referenced in function "void __cdecl drawScene(void)" (?drawScene@@YAXXZ)
1>gltools test.obj : error LNK2019: unresolved external symbol "public: void __thiscall GLBatch::End(void)" (?End@GLBatch@@QAEXXZ) referenced in function "void __cdecl setupScene(void)" (?setupScene@@YAXXZ)
1>gltools test.obj : error LNK2019: unresolved external symbol "public: void __thiscall GLBatch::Begin(unsigned int,unsigned int,unsigned int)" (?Begin@GLBatch@@QAEXIII@Z) referenced in function "void __cdecl setupScene(void)" (?setupScene@@YAXXZ)
1>gltools test.obj : error LNK2019: unresolved external symbol "public: bool __thiscall GLShaderManager::InitializeStockShaders(void)" (?InitializeStockShaders@GLShaderManager@@QAE_NXZ) referenced in function "void __cdecl setupScene(void)" (?setupScene@@YAXXZ)
1>gltools test.obj : error LNK2019: unresolved external symbol "public: void __thiscall GLBatch::CopyVertexData3f(float (*)[3])" (?CopyVertexData3f@GLBatch@@QAEXPAY02M@Z) referenced in function "public: void __thiscall GLBatch::CopyVertexData3f(float *)" (?CopyVertexData3f@GLBatch@@QAEXPAM@Z)
1>gltools test.obj : error LNK2019: unresolved external symbol "void __cdecl gltSetWorkingDirectory(char const *)" (?gltSetWorkingDirectory@@YAXPBD@Z) referenced in function _main
1>gltools test.obj : error LNK2019: unresolved external symbol "public: __thiscall GLBatch::GLBatch(void)" (??0GLBatch@@QAE@XZ) referenced in function "void __cdecl `dynamic initializer for 'a''(void)" (??__Ea@@YAXXZ)
1>gltools test.obj : error LNK2019: unresolved external symbol "public: __thiscall GLShaderManager::GLShaderManager(void)" (??0GLShaderManager@@QAE@XZ) referenced in function "void __cdecl `dynamic initializer for 'b''(void)" (??__Eb@@YAXXZ)
1>gltools test.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall GLBatch::~GLBatch(void)" (??1GLBatch@@UAE@XZ) referenced in function "void __cdecl `dynamic atexit destructor for 'a''(void)" (??__Fa@@YAXXZ)
1>gltools test.obj : error LNK2019: unresolved external symbol "public: __thiscall GLShaderManager::~GLShaderManager(void)" (??1GLShaderManager@@QAE@XZ) referenced in function "void __cdecl `dynamic atexit destructor for 'b''(void)" (??__Fb@@YAXXZ)

Anyone know why this is happening?

Dani AI

Generated

Short summary: the original LNK2019s came from the GLTools symbols not being linked; correctly pointed toward missing OpenGL/GLTools libraries and resolved the link errors by adding gltools.lib. The later runtime crash (access violation on a call into GLShaderManager) is a separate problem and is almost always caused by a bad call into a variadic API or an ABI/runtime mismatch between the app and the library.

Key cause and fix (concise): GLShaderManager::UseStockShader is a variadic function. Some stock shaders (for example the identity shader) expect a pointer to a 4-component RGBA float. Calling UseStockShader with only the shader id and no extra parameter yields undefined behavior and typically crashes when the function reads a non-existent argument. Also confirm that the GL context exists and that GLEW was initialized before any shader initialization; InitializeStockShaders must run after creating the window and after glewInit. Inspect the debugger at the exception to see whether the crash happens inside UseStockShader or earlier during initialization.

Additional troubleshooting checklist: verify the linked .lib matches the toolchain (same compiler and MSVC runtime, Debug vs Release) and platform (Win32 vs x64); ABI mismatches often produce invalid vtables and immediate crashes. If using static libs, make sure the matching static macros and libraries are used (for example static/freeglut or static GLEW choices). If prebuilt libs are suspect, rebuild GLTools with the same compiler/settings as the project. Finally, break on the exception and inspect the shaderManager object and its vtable—an invalid vtable strongly indicates a linking/ABI mismatch rather than a logic bug.

Recommended Answers

All 6 Replies

you have to add some openGL libraries. Look in openGL docs for one of those functions and it should tell you what libraries you have to use.

use your compiler's debugger and find out what caused that problem.

I tried that but I still can't figure out how to fix it. It's not letting me create any arrays. I tried dynamically allocating the memory but it still didn't work :/

It allowed me to create the same array in main, but not in setupScene.

Hmm, I remade the project and copied everything the book said but now I'm getting "Unhandled exception at 0x02c6ee05 in gltools_4.exe: 0xC0000005: Access violation reading location 0x10624dd3." when the program tries to run this :/

shaderManager.UseStockShader(GLT_SHADER_IDENTITY);

Also, this, because it might be important. http://i.imgur.com/VMFAT.png

Program code:

#include <GLTools.h>
#include <GLShaderManager.h>

#ifdef __APPLE__
#include <glut/glut.h>
#else
#define FREEGLUT_STATIC
#include <GL/glut.h>
#endif

GLBatch triangleBatch;
GLShaderManager shaderManager;

void ChangeSize(int w, int h){
    glViewport(0, 0, w, h);
}

void setupRC(){
    glClearColor(0.0f, 0.0f, 1.0f, 1.0f);

    shaderManager.InitializeStockShaders();

    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();
}

void RenderScene(void){
    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);
    triangleBatch.Draw();

    glutSwapBuffers();
}


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;
}

Anyone know how to fix it? :/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.