2014-11-04 75 views
0

我有一个应用程序,其版本号在C/C++头文件被指定时,version.h如何从C++头文件中将字符串提取到NSIS变量中?

#ifndef VERSION_H 
#define VERSION_H 

#define APPLICATION_VERSION "1.2.3" 

#endif 

版本号也是在NSIS脚本中指定,

!ifndef VERSION 
!define VERSION "1.2.3" 
!endif 

使得这个字符串可以包含在安装程序的名称中。有没有办法让NSIS脚本直接从version.h中读取这个值,这样我只需要在一个地方指定版本号?

回答

1

使用!searchparse!searchreplace!define /math阅读在编译时/操控版本号:

!searchparse /file "c:\some\path\version.h" `#define APPLICATION_VERSION "` VERSION `"` 

下一个示例使用的是临时文件,以便它可以在没有外部的依赖关系进行测试:

!tempfile testhdr 
!appendfile "${testhdr}" "#ifndef VERSION_H$\n" 
!appendfile "${testhdr}" "#define VERSION_H$\n" 
!appendfile "${testhdr}" '#define APPLICATION_VERSION "1.2.3"$\n' 
!appendfile "${testhdr}" "#endif$\n" 

!searchparse /file "${testhdr}" `#define APPLICATION_VERSION "` VER_MAJOR `.` VER_MINOR `.` VER_BUILD `"` 
!searchparse /file "${testhdr}" `#define APPLICATION_VERSION "` VER_STRING `"` 

!delfile "${testhdr}" ; Cleanup 

!warning "${VER_MAJOR}, ${VER_MINOR}, ${VER_BUILD}" 
!error "${VER_STRING}" 

也可以使用既是有效的C也是NSIS代码的定义来编写标题:

#define /* 
!define /**/ \ 
FOO "Bar" 
+0

此代码似乎创建了一个临时文件...您能提供一个从现有文件中读取文本的例子,就像我的问题一样吗? – bdesham 2014-11-05 13:26:55

+0

这只是为了让这个例子在没有外部文件的机器上工作,只需用'!searchparse/file“c:\ foo \ bar \ version.h”'替换'!searchparse/file“$ {testhdr}删除所有与testhdr相关的代码... – Anders 2014-11-05 13:29:08

相关问题