【发布时间】:2015-08-03 12:03:05
【问题描述】:
我正在学习一些 openGL 教程,因为它们都有 C++ 语法,我需要将它们转换为 C 语法,而且我在使用全局变量时遇到了一些问题。
所以我在共享头文件 LUtil.h 中有我的 extern 声明
#ifndef LUTIL_H
#define LUTIL_H
#include "LOpenGL.h"
#include <stdio.h>
#include <stdbool.h>
//Color modes
extern const int COLOR_MODE_CYAN;
extern const int COLOR_MODE_MULTI;
//Screen constants
extern const int SCREEN_WIDTH;
extern const int SCREEN_HEIGHT;
extern const int SCREEN_FPS;
extern int gColorMode;
extern GLfloat gProjectionScale;
...
我有我的 LUtil.c 文件,声明发生在其中
#include "LUtil.h"
//The current color rendering mode
const int COLOR_MODE_CYAN = 0;
const int COLOR_MODE_MULTI = 1;
//constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_FPS = 60;
//The projection scale
int gColorMode = 0;
GLfloat gProjectionScale = 1.f;
...
现在,如果我这样编译它就可以了。但是如果我在 LUtil.c 中像这样初始化 gColorMode 常量
int gColorMode = COLOR_MODE_CYAN;
我收到一个编译器错误,说我的初始化程序不是常量,尽管我已经声明了 COLOR_MODE_CYAN 和 const 并使用它进行了初始化。
这是为什么?
【问题讨论】:
标签: c initialization constants