C++ 使用另一个文件的const变量,必须声明和定义都带上extern限定符,因为const变量默认仅在文件内可见。
value.h
#ifndef VALUE_H
#define VALUE_H
extern const int value; // 声明
#endif // !VALUE_H
value.cpp
extern const int value = 123; // 定义
main.cpp
#include <iostream>
#include "value.h"
using namespace std;
int main() {
cout << value << endl;
return 0;
}