-3

I'm currently developing an OpenGL framework for video games. This framework contains a specific program that loads shaders. In the class of said program I have these three functions:

InitShaderProgram(...);
CreateShader(...);
CreateProgram(...);

The InitShaderProgram calls CreateShader and CreateProgram in such way:

bool ShaderLoader::InitShaderProgram(GLuint &Program)
{
    std::vector<GLuint> ShaderList;

    ShaderList.push_back(ShaderLoader::CreateShader(GL_VERTEX_SHADER, VertexShader));
    ShaderList.push_back(ShaderLoader::CreateShader(GL_FRAGMENT_SHADER, FragmentShader));

    Program = ShaderLoader::CreateProgram(ShaderList);
    std::for_each(ShaderList.begin(), ShaderList.end(), glDeleteShader);

    return GL_TRUE;
}

However, whenever I try to compile this code, it gives me two "unresolved external symbol" errors:

Error   4   error LNK2019: unresolved external symbol "public: virtual unsigned int __thiscall ShaderLoader::CreateProgram(class std::vector<unsigned int,class std::allocator<unsigned int> > const &)" (?CreateProgram@ShaderLoader@@UAEIABV?$vector@IV?$allocator@I@std@@@std@@@Z) referenced in function "public: bool __thiscall ShaderLoader::InitShaderProgram(unsigned int &)" (?InitShaderProgram@ShaderLoader@@QAE_NAAI@Z)

Error   3   error LNK2019: unresolved external symbol "public: virtual unsigned int __thiscall ShaderLoader::CreateShader(unsigned int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?CreateShader@ShaderLoader@@UAEIIABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: bool __thiscall ShaderLoader::InitShaderProgram(unsigned int &)" (?InitShaderProgram@ShaderLoader@@QAE_NAAI@Z)

In my header file, I define these three functions as such:

bool   InitShaderProgram(GLuint&);
GLuint CreateShader(GLenum, const std::string&);
GLuint CreateProgram(const std::vector<GLuint>&);

If I get this right (I most likely don't), the compiler does not understand where these functions come from. Can anyone help me out here?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
valtari
  • 347
  • 1
  • 3
  • 10
  • How are you compiling? – Joseph Mansfield Mar 21 '13 at 16:11
  • 3
    Just like in all the other questions about the same topic: you did not link to the definition of the symbols. figure out where they live, link them, done. – PlasmaHH Mar 21 '13 at 16:13
  • These functions live in the same class as the InitShaderProgram() function does, doesn't that make them linked by default? – valtari Mar 21 '13 at 16:17
  • @Jakobson "live in the class" is too vague a term to answer. Those functions must be compiled by the compiler, and the resulting object file(s) must be passed to the linker. One of those two things isn't happening for you. – Drew Dormann Mar 21 '13 at 16:21

2 Answers2

2

Unresolved External Symbol means the compiler has looked at the code you have written and has generated all code in the source file you are compiling and a set of symbols it needs for the linker to look up. The linker takes all of the object code files and maps all of the defined symbols from one translation unit (think source file and its headers) to all of the symbols. When it can't find a implemented symbol it emits an Unresolved External Symbol.

In general this is because you have failed to include the library object file in the linkers list of object files. The other likly cause if you have declared a function and not defined it.

Emits an undefined Symbol

main.cpp

int foo(); 

int main(int argc, char ** argv)
{
   return foo();
}

This will complain because I have not defined foo.

Community
  • 1
  • 1
rerun
  • 25,014
  • 6
  • 48
  • 78
1

If I get this right (I most likely don't), the compiler does not understand where these functions come from.

Unfortunally no. The error is not from compiler but from linker.

Do you have already implemented this two functions: ShaderLoader::CreateShader and ShaderLoader::CreateProgram? Or just declarated in a header? How do you link?

Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92