Fragment shader compilation error: syntax error : unexpected $end

I am following the tuts from learnopengl.com and have written a shader class.

The fragment shader is:


out vec4 FragColor;

in vec3 ourColor;
in vec2 TexCoord;

uniform sampler2D texture1;
uniform sampler2D texture2;

void main()
{
    FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2);
}

TexCoord isn’t used but is there in the vertex data.

I load the shader from a file to a char array and then use glShaderSource as:


glShaderSource(_handle, 1, &tmp, NULL);

where tmp is the char array and _handle is the id.

The compilation gives an error:


[ERROR]       resources/shaders/frag1.frag : compilation failed

0:1(1): error: syntax error, unexpected $end

What is this error and how to fix it?

OpenGL 3.3 is supported on my hardware.

Quick google search gave me this:

https://stackoverflow.com/questions/28527956/get-011-error-syntax-error-unexpected-end-when-trying-to-compile-shader

What it says is, that the passed shader code data (in your case &tmp) is cleared/or invalid. If you have a look into the stack overflow problem you see that his readfile function stores the data into a std::string somewhere on the way. Then he stores the pointer to the strings internal char string to a temporary pointer (two times ô.O) and returns it. The problem here is not the temporary pointer but the fact, that it is pointing to data that is managed by a temporary string which is destroyed at the end of the function.
So do a std::cout on tmp before the function call:


glShaderSource(_handle, 1, &tmp, NULL);

Another issue might be a missing zero termination character ( \0 ) at the end of your char array. If std::cout does print something, just add the /0 to your array.

Greetings

Is tmp an array (char tmp[]) or a pointer (char *tmp)? If it’s an array, the above won’t work, and your compiler should have warned you about it (assuming that warnings are enabled). You’d need e.g.


char *p = tmp;
glShaderSource(_handle, 1, &p, NULL);