0

Source1.cpp(128): error C3861: 'InitShader': identifier not found我有 2 个文件并且一个是InitShader.cpp. 两个文件都编译。我相信这是 Visual Studio 中的一些设置,可以让一个文件找到另一个文件,因为 InitShader 被另一个源文件引用。该项目在 VS 2012 中看起来像这样

在此处输入图像描述

代码是

#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
4

1 回答 1

1

The error sounds like it's coming from Source1.cpp not being communicating properly with InitShader.cpp

Since there doesn't appear to be a InitShader.h, I'm assuming you're trying to #include in Source1.cpp, which probably is where the error comes in, since it's expecting a header file.

I'd say try moving all your function definition from InitShader.cpp over to an InitShader.h file, and try including that in Source1.cpp. This is my best guess without seeing the actual code in Source1.cpp.


Alternate idea:

It appears that you're providing the function body in InitShader.cpp for the function prototypes I assume in Angel.h. Are you declaring functions in Angel.h that you then define in InitShader.cpp? If so, I'd highly recomend creating an Angel.cpp file and defining the Angel functions there instead. It would be much cleaner, and I have a hunch that it might end up being the problem you're facing.

于 2013-06-09T08:41:01.687 回答