在VS2012中构建错误

时间:2013-06-09 03:44:45

标签: c++ visual-c++ opengl shader

当我有2个文件而其中一个是Source1.cpp(128): error C3861: 'InitShader': identifier not found时,请帮我解决错误InitShader.cpp。两个文件都编译。我相信Visual Studio中的一些设置使一个文件找到另一个,因为InitShader被另一个源文件引用。该项目在VS 2012中看起来像这样。

enter image description here

代码是

#include "Angel.h"

namespace Angel {

// Create a NULL-terminated string by reading the provided file
static char*
readShaderSource(const char* shaderFile)
{
    FILE* fp = fopen(shaderFile, "r");

    if ( fp == NULL ) { return NULL; }

    fseek(fp, 0L, SEEK_END);
    long size = ftell(fp);

    fseek(fp, 0L, SEEK_SET);
    char* buf = new char[size + 1];
    fread(buf, 1, size, fp);

    buf[size] = '\0';
    fclose(fp);

    return buf;
}


// Create a GLSL program object from vertex and fragment shader files
GLuint
InitShader(const char* vShaderFile, const char* fShaderFile)
{
    struct Shader {
    const char*  filename;
    GLenum       type;
    GLchar*      source;
    }  shaders[2] = {
    { vShaderFile, GL_VERTEX_SHADER, NULL },
    { fShaderFile, GL_FRAGMENT_SHADER, NULL }
    };

    GLuint program = glCreateProgram();


    for ( int i = 0; i < 2; ++i ) {
    Shader& s = shaders[i];
    s.source = readShaderSource( s.filename );
    if ( shaders[i].source == NULL ) {
        std::cerr << "Failed to read " << s.filename << std::endl;
        exit( EXIT_FAILURE );
    }

    GLuint shader = glCreateShader( s.type );

    glShaderSource( shader, 1, (const GLchar**) &s.source, NULL );
    glCompileShader( shader );

    GLint  compiled;
    glGetShaderiv( shader, GL_COMPILE_STATUS, &compiled );
    if ( !compiled ) {
        std::cerr << s.filename << " failed to compile:" << std::endl;
        GLint  logSize;
        glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &logSize );
        char* logMsg = new char[logSize];
        glGetShaderInfoLog( shader, logSize, NULL, logMsg );
        std::cerr << logMsg << std::endl;
        delete [] logMsg;

        exit( EXIT_FAILURE );
    }

    delete [] s.source;

    glAttachShader( program, shader );
    }

    /* link  and error check */
    glLinkProgram(program);

    GLint  linked;
    glGetProgramiv( program, GL_LINK_STATUS, &linked );
    if ( !linked ) {
    std::cerr << "Shader program failed to link" << std::endl;
    GLint  logSize;
    glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logSize);
    char* logMsg = new char[logSize];
    glGetProgramInfoLog( program, logSize, NULL, logMsg );
    std::cerr << logMsg << std::endl;
    delete [] logMsg;

    exit( EXIT_FAILURE );
    }

    /* use program object */
    glUseProgram(program);

    return program;
}

}  // Close namespace Angel block

1 个答案:

答案 0 :(得分:1)

这个错误听起来像来自Source1.cpp没有与InitShader.cpp正常通信

由于似乎没有InitShader.h,我假设您在Source1.cpp中尝试#include,这可能是错误的来源,因为它需要一个头文件。

我想尝试将所有函数定义从InitShader.cpp移动到InitShader.h文件,并尝试将其包含在Source1.cpp中。这是我最好的猜测,没有看到Source1.cpp中的实际代码。


替代想法:

看来你在InitShader.cpp中为我在Angel.h中假设的函数原型提供函数体。你是否在Angel.h中声明了你在InitShader.cpp中定义的函数?如果是这样,我会高度推荐创建一个Angel.cpp文件并在那里定义Angel函数。它会更清洁,我预感到它可能最终成为你所面临的问题。