Initializer element is not a compile-time constant c

Viewed 189

i don't know why i am getting this error but my code do not complies and throws the error Initializer element is not a compile-time constant here is the code

#include <math.h>
#include <stdio.h>

const float near = 0.1f;
const int fov_angle = 90;
const float far = 1000.0f;
const float width = 800.0f;
const float height = 600.0f;
const float aspect_ratio = width / height; 
const float fov = (1.0f / tan(fov_angle / 2.0)); <- error

float projMat[4][4] = {
  {fov * aspect_ratio, 0, 0, 0}, <- error
  {0,                fov, 0, 0},
  {0, 0, ((far+near)/(far-near)), 1},
  {0, 0, ((2*near*far)/(near-far)), 0}
};


1 Answers

As the error message states, the initializer for fov, which is a global variable, cannot be computed at compile time because it includes a function call. Executable code may not reside outside of any functions. projMat has a similar problem because it depends on fov.

You'll need to set the values of these variables inside of a function, probably main.

Related