gpt4 book ai didi

c++ - 如何解决这个编译错误

转载 作者:太空宇宙 更新时间:2023-11-04 07:27:15 24 4
gpt4 key购买 nike

我正在尝试编译一些 OpenGl 代码,因为我遇到了这个错误

1>c:\users\student\documents\visual studio 2012\projects\openglproject1\openglproject1\initshader.h(10):错误 C4996:“fopen”:此函数或变量可能不安全。考虑改用 fopen_s。要禁用弃用,请使用 _CRT_SECURE_NO_WARNINGS。详情请参见在线帮助。
程序是

#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

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

最佳答案

你可以定义_CRT_SECURE_NO_WARNINGS在产生错误的头文件中,在包含头文件之前或通过将其添加到“项目->属性->配置属性->C/C++->Preprocessor->Preprocessor Definitions”中的项目设置中将其应用于整个项目".

#define _CRT_SECURE_NO_WARNINGS
#include "initshader.h"

//... your code here

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

关于c++ - 如何解决这个编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17246234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com