Unresolved external symbol (OpenGL and c++)

2019-07-13 10:33发布

问题:

OK, so I'm writing a little project, nothing complex, it just has several classes. As the title implies, it uses OpenGL. At the moment, there's no "real" main function. I have included glew.h wherever I've used gl* function calls, and added to the linker input glew32.lib.

And yet, it gives me this:

Error 2 error LNK2019: unresolved external symbol _imp_glBindTexture@8 referenced in function "public: void __thiscall Texture2D::Bind(unsigned int)" (?Bind@Texture2D@@QAEXI@Z) Texture.obj Licenta

... and a host of other unresolved external symbol errors regarding OpenGL texture functions. But it doesn't complain about this:

glBindVertexArray(m_VAO);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Buffers[INDEX_BUFFER]);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices[0]) * Indices.size(), &Indices[0], GL_STATIC_DRAW);

or this:

glDrawElementsBaseVertex(GL_TRIANGLES, 
                             m_Entries[i].NumIndices, 
                             GL_UNSIGNED_INT, 
                             (void*)(sizeof(unsigned int) * m_Entries[i].BaseIndex), 
                             m_Entries[i].BaseVertex);

So, what's the deal? If one gl* function call failed linking, wouldn't ALL have to fail?

回答1:

glBindTexture is a "core" OpenGL feature. This function resides in opengl32.dll, so just add the opengl32.lib to your linker input.

glDrawElementsBaseVertex and glBindVertexArray are extensions and GLEW defines these as function pointers (with dynamic late binding done in runtime), thus no "unresolved symbol" errors.



回答2:

You'll need to include the library in the linkers input.

For VS:

  1. Put the library's file in the solution's root folder. (you might not have to do this in general for some libraries)
  2. At Visual Studios menu go to Project --> [SolutionName] properties.
  3. A dialog will open. navigate to Configuration Properties --> Linker --> Input
  4. At "Additional Dependencies" add the name of the library. (it'll probably be *.lib)