Random errors in Shader Compilation or Linkage

Hi! I need some help… (using OpenGL version 3.3)

In my code, i have the following shaders:

Vertex shader


#version 330 core

layout (location=0) in vec3 position;
layout (location=1) in vec3 in_color;

out vec3 f_color;
out gl_PerVertex{
	vec4 gl_Position;
};

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main(){
	gl_Position = projection * view * model * vec4(position,1.0f);
	f_color = in_color;
}

Fragment shader


#version 330 core

in vec3 f_color;

out vec4 out_color;

void main(){
	out_color=vec4(f_color, 1.0f);
}

Another vertex shader


#version 330 core

layout (location=0) in vec3 position;

out gl_PerVertex{
	vec4 gl_Position;
};

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main(){
	gl_Position = projection * view * model * vec4(position,1.0f);
}

Another Fragment shader


#version 330 core

out vec4 out_color;

void main(){
	out_color=vec4(1.0f);
}

I get some weird random errors when i try to compile and link the shaders (Each Vertex shader has its own Fragment Shader)

I say they are random because if I keep trying to run my code, it eventually compile and link without problems.
Some of the errors are:

(this is in the first Vertex shader code I mentioned before)


Vertex shader failed to compile with the following errors:
ERROR: 0:17: error(#143) Undeclared identifier: f_colo
ERROR: 0:17: error(#131) Syntax error: pre-mature EOF parse error
ERROR: error(#273) 2 compilation errors.  No code generated


Vertex shader failed to compile with the following errors:
ERROR: 0:1: error(#132) Syntax error: "P" parse error
ERROR: error(#273) 1 compilation errors.  No code generated


Vertex shader failed to compile with the following errors:
ERROR: 0:16: error(#143) Undeclared identifier: f_coloQ
ERROR: 0:16: error(#131) Syntax error: pre-mature EOF parse error
ERROR: error(#273) 2 compilation errors.  No code generated


Vertex shader failed to compile with the following errors:
ERROR: 0:17: error(#143) Undeclared identifier: f_coloa
ERROR: 0:17: error(#143) Undeclared identifier: undefined
ERROR: 0:17: error(#131) Syntax error: pre-mature EOF parse error
ERROR: error(#273) 3 compilation errors.  No code generated


Vertex shader failed to compile with the following errors:
ERROR: 0:17: error(#143) Undeclared identifier: f_colo
ERROR: 0:17: error(#143) Undeclared identifier: undefined
ERROR: 0:17: error(#132) Syntax error: "<" parse error
ERROR: error(#273) 3 compilation errors.  No code generated

I googled it, and found that my method of reading the file might be wrong and might be getting some random data from buffer. I changed it many times, but nothing solved it.
Now it is like this:


...
int size;
const char* code = readFile(file, &size);
glShaderSource(shader, 1, &code, &size);
glCompileShader(shader);
...

const char* Shader::readFile(std::string name, int* size){
	name.insert(0,"Shader/"); // The shader is inside the folder

	std::string code;
	std::ifstream file;

	file.exceptions(std::ifstream::badbit);

	try{
		file.open(name.c_str());
		std::stringstream filestream;
		filestream << file.rdbuf();
		file.close();
		code = filestream.str();
	}
	catch(std::ifstream::failure e){
		std::cout << "Error Shader" << std::endl;
	}

	(*size) = code.length();
	return code.c_str();
}

I noticed that most errors happen with f_color. I tried to rewrite this line with gedit, emacs, Visual Studio Code and even with nano. The errors just changes to another line (sometimes changes to another shader).
I tried to rewrite everything, but it keeps finding errors.

I also noticed that even with these errors, the code runs with no problem (if i skip the part where I check for compilation and linkage errors)

My system info:
Integrated Intel HD Graphics 4000
AMD HD Radeon 7730M
OpenGL version string: 4.5.13399 Compatibility Profile Context 15.201.1151
Ubuntu 14.04 (proprietary video driver)

I do not have experience in coding, so I might be doing something really stupid and not seeing it hahaha
Does anyone know what is happening?

EDIT:
I don’t know what happened.
Now when I ignore the errors it still works, but some weird thing happens (some shader isn’t working)

This the load shader function i use try it :


Shader::LoadShader(const std::string &filePath)
{
	std::ifstream file;
	file.open((filePath).c_str());
	std::string output;
	std::string line;

	if(!file.is_open())
	{
		std::cerr << " Enable to load shader: "<<filePath<<"...!!"<<std::endl;
		exit (EXIT_FAILURE);
	}

	while(file.good())
	{
		getline(file,line);
		output.append(line + "
");
	}

	file.close();
	return output;
}

You should learn about scope of variables.
And also about setting breakpoints and examining memory.

It worked! :smiley:

The problem really was my function hahahaha
Just for tests, I created an char array with malloc, and returned the array. Now it works like a charm
I had no idea of how the c_str() works, and never thought about the scope of it

Thank you very much! :slight_smile:

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.