2013-03-21 56 views
-3

我目前正在为视频游戏开发一个OpenGL框架。该框架包含加载着色器的特定程序。在课堂上说,程序的我有三个功能:另一个“未解析的外部符号”错误

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

的InitShaderProgram调用CreateShader和CreateProgram以这样的方式:

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

然而,每当我试图编译这段代码,它给了我两个“解析的外部符号”错误:

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 &)" ([email protected]@@[email protected][email protected]@[email protected]@@[email protected]@@Z) referenced in function "public: bool __thiscall ShaderLoader::InitShaderProgram(unsigned int &)" ([email protected]@@[email protected]) 

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 &)" ([email protected]@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@Z) referenced in function "public: bool __thiscall ShaderLoader::InitShaderProgram(unsigned int &)" ([email protected]@@[email protected]) 

在我的头文件中,我定义这三个功能,例如:

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

如果我得到这个权利(我很可能不),编译器不明白这些函数来自哪里。有人可以帮我从这里出去吗?

+0

你是如何编译的? – 2013-03-21 16:11:14

+3

就像关于同一主题的所有其他问题一样:您没有链接到符号的定义。找出他们居住的地方,把他们联系起来,完成。 – PlasmaHH 2013-03-21 16:13:23

+0

这些函数与InitShaderProgram()函数存在于同一个类中,不会使它们默认链接? – valtari 2013-03-21 16:17:17

回答

1

如果我得到这个权利(我很可能不),编译器不明白这些函数来自哪里。

不幸的是没有。错误不是来自编译器,而是来自链接器。

您是否已经实现了这两个函数:ShaderLoader :: CreateShader和ShaderLoader :: CreateProgram?或者只是在头文件中声明?你如何链接?

+1

这不是一个答案... – 2013-03-21 16:28:26

2

无法解析的外部符号表示编译器查看了您编写的代码,并生成了您正在编译的源文件中的所有代码以及链接器查找所需的一组符号。链接器获取所有目标代码文件,并将所有定义的符号从一个translation unit(认为源文件及其标题)映射到所有符号。当它找不到实现的符号时,它会发出未解析的外部符号。

一般情况下,这是因为您未能将库对象文件包含在对象文件的链接程序列表中。另一个原因是如果你已经声明了一个函数而没有定义它。

发出一个未定义的符号

的main.cpp

int foo(); 

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

这会抱怨,因为我还没有定义FOO。

相关问题