OpenGL returns -1 for an uniform that does exist and is used
<p>I have this vertex shader:</p>

<pre><code>#version 430 core

layout (location = 0) in vec3 position;
layout (location = 1) in vec2 textureCoordinate;
layout (location = 2) in vec3 normal;
layout (location = 4) in vec3 tangent;

uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;
uniform vec3 lightPosition = vec3(0.0, 1.0, 0.0);

out vec2 texCoord;
out vec3 lightDirection;
out vec3 eyeDirection;

void main() {
    vec4 P = modelViewMatrix * vec4(position, 1.0);
    vec3 N = normalize(mat3(modelViewMatrix) * normal);
    vec3 T = normalize(mat3(modelViewMatrix) * tangent);
    vec3 B = cross(N, T);
    vec3 L = lightPosition - P.xyz;
    vec3 V = -P.xyz;

    lightDirection = normalize(vec3(dot(V, T), dot(V, B), dot(V, N)));
    eyeDirection = normalize(vec3(dot(V, T), dot(V, B), dot(V, N)));
    texCoord = textureCoordinate;

    gl_Position = projectionMatrix * P;
}
</code></pre>

<p>And this fragment shader:</p>

<pre><code>#version 430 core

in vec2 texCoord;
in vec3 lightDirection;
in vec3 eyeDirection;

layout (binding = 0) uniform sampler2D tex;
layout (binding = 1) uniform sampler2D normalMap;

out vec4 color;

void main() {
    vec3 ambient = vec3(0.1);

    vec3 V = normalize(eyeDirection);
    vec3 L = normalize(lightDirection);
    vec3 N = normalize(texture(normalMap, texCoord)).rgb * 2.0 - vec3(1.0);
    vec3 R = reflect(-L, N);

    vec3 diffuseAlbedo = texture(tex, texCoord).rgb;
    /*vec3 diffuseAlbedo = vec3(1.0);*/
    vec3 diffuse = max(dot(N, L), 0.0) * diffuseAlbedo;

    vec3 specular = vec3(0.0);

    color = vec4(ambient + diffuse + specular, 1.0);
}
</code></pre>

<p>As you can see, the variable <code>lightPosition</code> is actually used in the calculations. But when I want to get the location of it via <code>glGetUniformLocation</code>, I get <code>-1</code>.</p>

<p>I looked through the other questions here on SO, like those ones:</p>

<ul>
<li><a href="https://stackoverflow.com/questions/35690994/glgetactiveuniform-reports-uniform-exists-but-glgetuniformlocation-returns-1">glGetActiveUniform reports uniform exists, but glGetUniformLocation returns -1</a></li>
<li><a href="https://stackoverflow.com/questions/13486199/glgetuniformlocation-return-1-on-nvidia-cards">glGetUniformLocation return -1 on nvidia cards</a></li>
<li><a href="https://stackoverflow.com/questions/23319288/glgetuniformlocation-returning-1-even-though-used-in-vertex-shader">glGetUniformLocation() returning -1 even though used in vertex shader</a></li>
</ul>

<p>Especially the last one - but in my case, <code>lightPosition</code> is used to calculate <code>lightDirection</code> and this is then used in the fragment shader. So it should not be removed.</p>

<p>Any ideas on what is going on here? Or how to debug this?</p>

<p>Also: I set a default value for the uniform. If I remove the default value, it still gives <code>-1</code>.</p>

<p>I am running Ubuntu 15.10 with Nvidia 340.96 drivers and a GF 710M card. I am running OpenGL 4.4 with core profile.</p>