我的项目使用version.h
配置应用程序版本,许多源文件包括此version.h
,当前它定义了应用程序版本,例如:
#define VERSION 1
每次升级到新版本时,我都需要修改此VERSION
,并且因为所有源文件都包含了整个项目,整个项目都需要很长时间。
所以我想将其分为 .h 和 .cpp 。然后,我只是修改 .cpp 时,它只会重新编译一个文件。
这是我尝试的:
test.cpp
#include <iostream>
using namespace std;
const static int VERSION;
// some other source code that uses the version
struct traits {
const static int ver = VERSION;
};
int main() {
cout << traits::ver << endl;
}
version.cpp
const int VERSION = 1;
请注意,我需要将其用作静态。但是它没有编译,错误:
错误C2734:'版本':'const'对象必须初始化,如果没有初始化 '外部'
错误C2131:表达未评估为常数
注意:失败是由非恒定参数引起的,或引用 非恒定符号
注意:请参阅"版本"的用法
定义版本代码的最佳方法是什么?
环境:Visual Studio 2015更新3
version.h
extern const int VERSION;
version.cpp
#include "version.h"
extern const int VERSION = 1;
test.cpp
#include "version.h"
struct traits {
const static int ver;
};
const int traits::ver = VERSION;
wandbox
我正在使用类似的东西。
我的 version.cpp 看起来像:
const int SoftwareVersion = 0xAA55A55A;
要使用版本号(例如在main.cpp中(,它看起来像:
...
extern const int SoftwareVersion;
...
int main(int argc, char **args) {
printf("Version %in",SoftwareVersion);
}