如何解决此编译错误

Posted

技术标签:

【中文标题】如何解决此编译错误【英文标题】:How to resolve this compilation error 【发布时间】:2013-06-22 00:43:10 【问题描述】:

当我收到此错误时,我正在尝试编译一些 OpenGl 代码

1>c:\users\student\documents\visual studio 2012\projects\openglproject1\openglproject1\initshader.h(10): error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 该程序是

#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

你能告诉我我必须如何修改程序吗?

【问题讨论】:

看这个:***.com/q/2575116/10077 【参考方案1】:

您可以在生成错误的头文件中定义_CRT_SECURE_NO_WARNINGS,然后再包含头文件或通过将其添加到“Project->Properties->Configuration Properties->C中的项目设置将其应用到整个项目中/C++->预处理器->预处理器定义”。

#define _CRT_SECURE_NO_WARNINGS
#include "initshader.h"

//... your code here

如果您使用预编译的标头,并且生成错误的标头包含在 stdafx.h 中,则需要将其添加到 stdafx.h 标头中,或者如上所述添加到您的项目设置中。

【讨论】:

以上是关于如何解决此编译错误的主要内容,如果未能解决你的问题,请参考以下文章

如何解决 PL/Sql 函数编译错误

如何解决此 Visual Studio 编译器 BUG?

编译时出现ARC错误

为啥我的程序显示此编译错误?

Linux系统中,源码编译安装软件包编译过程有问题可以继续安装步骤吗?

如何解决此链接错误