简体   繁体   English

VS2012中的生成错误

[英]Build error in VS2012

Help me with the error Source1.cpp(128): error C3861: 'InitShader': identifier not found when I have 2 files and one is InitShader.cpp . 帮助我解决错误Source1.cpp(128): error C3861: 'InitShader': identifier not found当我有2个文件且一个是InitShader.cpp Both files compile. 这两个文件都会编译。 I believe that it is some setting in Visual Studio to make the one file find the other, since the InitShader is referenced by the other source file. 我相信这是Visual Studio中的一种设置,可以使一个文件找到另一个文件,因为InitShader被另一个源文件引用。 The project looks like this in VS 2012 VS 2012中的项目看起来像这样

在此处输入图片说明

The code is 该代码是

#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

The error sounds like it's coming from Source1.cpp not being communicating properly with InitShader.cpp 该错误听起来像是来自Source1.cpp的消息,无法与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. 由于似乎没有InitShader.h,因此我假设您正在尝试在Source1.cpp中包含#include,这可能是错误所在,因为它期望有头文件。

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. 我想说的是将所有函数定义从InitShader.cpp移到InitShader.h文件,并尝试将其包含在Source1.cpp中。 This is my best guess without seeing the actual code in Source1.cpp. 这是我的最佳猜测,而没有看到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. 您似乎在InitShader.cpp中提供了我在Angel.h中假定的函数原型的函数主体。 Are you declaring functions in Angel.h that you then define in InitShader.cpp? 您是否要在Angel.h中声明要在InitShader.cpp中定义的函数? If so, I'd highly recomend creating an Angel.cpp file and defining the Angel functions there instead. 如果是这样,我强烈建议您创建一个Angel.cpp文件并在那里定义Angel函数。 It would be much cleaner, and I have a hunch that it might end up being the problem you're facing. 这样会更清洁,我预感这可能最终成为您面临的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM