0
votes

I have been running into an error while trying to create and link a GLSL program. Whenever I try to link the program, I encounter an error.

Output:

ERROR: definition for "void main()" not found

Vertex shader:

#version 330 core

layout(location = 0) in vec3 vertex_modelspace;

void main()
{
gl_Position.xyz = vertex_modelspace;
gl_Position.w = 1.0;
}

Fragment shader:

#version 330 core

out vec3 color;

void main()
{
    color = vec3(1, 0, 0);
}

Shader program creation code:

ifstream vertStream(vertPath);
ifstream fragStream(fragPath);
string line;

while (!getline(vertStream, line))
{
    vertexShaderSource.append(line).append("\n");
}
vertStream.close();

while (!getline(fragStream, line))
{
    fragmentShaderSource.append(line).append("\n");
}
fragStream.close();

vertexShaderSourcePointer = vertexShaderSource.c_str();
fragmentShaderSourcePointer = fragmentShaderSource.c_str();

vertexShaderLength = vertexShaderSource.length();
fragmentShaderLength = fragmentShaderSource.length();

vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShaderID, 1, &vertexShaderSourcePointer, &vertexShaderLength);
glCompileShader(vertexShaderID);
checkShader(vertexShaderID);

fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShaderID, 1, &fragmentShaderSourcePointer, &fragmentShaderLength);
glCompileShader(fragmentShaderID);
checkShader(fragmentShaderID);

programID = glCreateProgram();
glAttachShader(programID, vertexShaderID);
glAttachShader(programID, fragmentShaderID);
glLinkProgram(programID);
checkProgram(programID);

The checkShader and checkProgram functions are simple error checks using glGetShaderiv and glGetProgramiv and printing the error log into stderr. I've checked whether the shader and program names are valid, and they are. I have also checked the compilation status of both shaders, and they compiled without a problem. This code has worked for me before (Using different bindings), and I have no clue why it isn't working.

By my understanding, this error message is meant to be shown when a shader doesn't have a main() function, but mine do. I'm pretty sure it's something stupid, but I've been staring at this all day to no avail. Help?

2
Did you look at the values in a debugger, and make sure that your shader source is properly read in? Otherwise, my best guess is that one or both of your shaders were not read in successfully. An empty shader might compile without error, but would then of course have no main() function. - Reto Koradi
Is it possible that your machine doesn't support GLSL version 330 core? Some GLSL compilers are so poor that they spit some random error messages if the version is not valid. - glampert
I have seen this question a lot recently. It usually turns out to be related to reading files line-by-line. Have you tried reading the entire file all at once as a binary blob? - Andon M. Coleman
@AndonM.Coleman I read the file all at once, and I'm not getting a linker error anymore. That being said, however, my shaders are not working. For some reason, when I call glUseProgram, it's like the program isn't being used at all. That's a separate problem, though. Thanks for helping! - Spaxxy
Well, that is a different story. You are writing your object-space vertices directly in the vertex shader... anything outside the range [-w,w] is going to be clipped. w is 1.0 in this case. There is a reason the output of gl_Position is called clip-space ;) - Andon M. Coleman

2 Answers

1
votes

If you feed the file in as one string without \n characters on the end of each line, you are likely to get this error.

-2
votes

You can check the file path of your shaders.

Change it to:

myshader.loadShaderCode("Resources/shaders/default.vs", "Resources/shaders/default.frag")

From:

myshader.loadShaderCode("Resources\shaders\default.vs", "Resources\shaders\default.frag")