Shaders & programsGL 2.0

glAttachShader

Adds a compiled shader to a program before linking.

Signature

void glAttachShader(GLuint program, GLuint shader);

How it works

A program is the set of shader stages the GPU runs together. Attach one shader per stage (at least a vertex and a fragment shader), then call glLinkProgram, which connects the vertex shader's outputs to the fragment shader's inputs by name and type.

After a successful link the shaders are no longer needed: detach and delete them, and the program keeps working.

Parameters

programGLuint

Target program.

shaderGLuint

Shader to attach. Only one shader per stage is normally attached.

Errors

When the call is invalid it does nothing and sets an error, which glGetError returns:

GL_INVALID_OPERATION
shader is already attached to program.

Example

example.cpp
1glAttachShader(program, vs);
2glAttachShader(program, fs);
Official Khronos documentation