0

I'm using VS2015, and trying to learn about opengl. But somehow I have some problems with the first example triangle.cpp which is in the opengl super bible 5th. I google 2 hours and still be stuck in here.

Here's the code:

// Triangle.cpp
// Our first OpenGL program that will just draw a triangle on the screen.

#include <Windows.h>
#include <GLTools.h>            // OpenGL toolkit
#include <GLShaderManager.h>    // Shader Manager Class
#include <GL\glew.h>
#ifdef __APPLE__
#include <glut/glut.h>          // OS X version of GLUT
#else
#define FREEGLUT_STATIC
#include <GL/glut.h>            // Windows FreeGlut equivalent
#endif

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

And I get these errors:

Error   LNK2019 unresolved external symbol "public: virtual __thiscall GLBatch::~GLBatch(void)" (??1GLBatch@@UAE@XZ) referenced in function "void __cdecl `dynamic atexit destructor for 'triangleBatch''(void)" (??__FtriangleBatch@@YAXXZ)    Opengl_Test C:\Users\sj88e\Documents\Visual Studio 2015\Projects\OpenGL_Test\Opengl_Test\main.obj   1   
Error   LNK2019 unresolved external symbol _glewInit@0 referenced in function _main Opengl_Test C:\Users\sj88e\Documents\Visual Studio 2015\Projects\OpenGL_Test\Opengl_Test\main.obj   1   
Error   LNK2019 unresolved external symbol _glewGetErrorString@4 referenced in function _main   Opengl_Test C:\Users\sj88e\Documents\Visual Studio 2015\Projects\OpenGL_Test\Opengl_Test\main.obj   1   
Error   LNK2019 unresolved external symbol "public: __thiscall GLBatch::GLBatch(void)" (??0GLBatch@@QAE@XZ) referenced in function "void __cdecl `dynamic initializer for 'triangleBatch''(void)" (??__EtriangleBatch@@YAXXZ)   Opengl_Test C:\Users\sj88e\Documents\Visual Studio 2015\Projects\OpenGL_Test\Opengl_Test\main.obj   1   
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 SetupRC(void)" (?SetupRC@@YAXXZ)   Opengl_Test C:\Users\sj88e\Documents\Visual Studio 2015\Projects\OpenGL_Test\Opengl_Test\main.obj   1   
Error   LNK2019 unresolved external symbol "public: void __thiscall GLBatch::End(void)" (?End@GLBatch@@QAEXXZ) referenced in function "void __cdecl SetupRC(void)" (?SetupRC@@YAXXZ)    Opengl_Test C:\Users\sj88e\Documents\Visual Studio 2015\Projects\OpenGL_Test\Opengl_Test\main.obj   1   
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)   Opengl_Test C:\Users\sj88e\Documents\Visual Studio 2015\Projects\OpenGL_Test\Opengl_Test\main.obj   1   
Error   LNK2019 unresolved external symbol "public: virtual void __thiscall GLBatch::Draw(void)" (?Draw@GLBatch@@UAEXXZ) referenced in function "void __cdecl RenderScene(void)" (?RenderScene@@YAXXZ)  Opengl_Test C:\Users\sj88e\Documents\Visual Studio 2015\Projects\OpenGL_Test\Opengl_Test\main.obj   1   
Error   LNK2019 unresolved external symbol "public: __thiscall GLShaderManager::GLShaderManager(void)" (??0GLShaderManager@@QAE@XZ) referenced in function "void __cdecl `dynamic initializer for 'shaderManager''(void)" (??__EshaderManager@@YAXXZ)   Opengl_Test C:\Users\sj88e\Documents\Visual Studio 2015\Projects\OpenGL_Test\Opengl_Test\main.obj   1   
Error   LNK2019 unresolved external symbol "public: __thiscall GLShaderManager::~GLShaderManager(void)" (??1GLShaderManager@@QAE@XZ) referenced in function "void __cdecl `dynamic atexit destructor for 'shaderManager''(void)" (??__FshaderManager@@YAXXZ)    Opengl_Test C:\Users\sj88e\Documents\Visual Studio 2015\Projects\OpenGL_Test\Opengl_Test\main.obj   1   
Error   LNK2019 unresolved external symbol "public: bool __thiscall GLShaderManager::InitializeStockShaders(void)" (?InitializeStockShaders@GLShaderManager@@QAE_NXZ) referenced in function "void __cdecl SetupRC(void)" (?SetupRC@@YAXXZ) Opengl_Test C:\Users\sj88e\Documents\Visual Studio 2015\Projects\OpenGL_Test\Opengl_Test\main.obj   1   
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 RenderScene(void)" (?RenderScene@@YAXXZ) Opengl_Test C:\Users\sj88e\Documents\Visual Studio 2015\Projects\OpenGL_Test\Opengl_Test\main.obj   1   
Error   LNK2019 unresolved external symbol "void __cdecl gltSetWorkingDirectory(char const *)" (?gltSetWorkingDirectory@@YAXPBD@Z) referenced in function _main Opengl_Test C:\Users\sj88e\Documents\Visual Studio 2015\Projects\OpenGL_Test\Opengl_Test\main.obj   1   
Error   LNK1120 13 unresolved externals Opengl_Test C:\Users\sj88e\Documents\Visual Studio 2015\Projects\OpenGL_Test\Debug\Opengl_Test.exe  1   

I've done all the instructions about include headers, libs and dlls. How can I solve this problem?

EDIT: I add all the header files in the VC/include folder and libs in the VC/lib folder.

Property Pages->VC++ Directories->include Directories

Property Pages->VC++ Directories->Library Directories

I think it can link by this setting, and I can run some examples in the 2nd edition book. Do I misunderstanding something?

Tim Lu
  • 3
  • 3
  • Your openGL libraries aren't linking, it's likely you either aren't linking them properly, or the compile/platform versions are wrong. You don't provide enough detail on either in your question – GPPK Oct 27 '17 at 14:41
  • The linker is unable to find corresponding static libraries – Asesh Oct 27 '17 at 14:41
  • `'ve done all the instructions about include headers, libs and dlls` is clearly not the case. Either add `GLShaderManager.cpp` and `GLTools.cpp` to your project or link against something that compiled them – PeterT Oct 27 '17 at 14:49
  • Sorry I am dumb, but I add gltools.lib in the Microsoft Visual Studio 14.0\VC\lib folder. Doesn't mean that I can compile the program? – Tim Lu Oct 27 '17 at 15:27
  • @TimLu no, you need to add gltools.lib to the list of files to link in the project settings as well – PeterT Oct 27 '17 at 15:33
  • or add the Microsoft Visual Studio specific `#pragma comment(lib, "gltools.lib")` line to the start of your source – PeterT Oct 27 '17 at 15:36
  • To your edit, no those settings are not enough, you need to add `gltools.lib` and the glew library file to the linker inputs in `Properties->Linker->Input->Additional Dependencies` – PeterT Oct 27 '17 at 18:03
  • I add gltools.lib and glew32s.lib in Properties->Linker->Input->Additional Dependencies, and it works. But I still don't know why vs2015 can't just find them when I put them in the VC++ Directories->Include Diretories. In my understanding, this path is default so it should work correctly. I still feel confused. – Tim Lu Oct 27 '17 at 19:28

0 Answers0