跨语言共享常量
我有一长串常量,需要在不同语言(Verilog、C、C++ 和 C#)的几个项目中访问这些常量。与其用每种语言重复它们,不如有一个好的方法来分享这些?
我唯一能想到的是一个文本文件和一个预处理脚本? 这是最好的解决方案还是有更简单/更优雅的解决方案?
I have a long list of constants that I need access to in several projects which are in different languages(Verilog, C, C++ and C#). Rather than repeating them in each language, is there a good way to share these?
The only thing I could think of would be a text file and a preprocessing script?
Is this the best solution or is there something easier/more elegant?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
自动更新代码中这些常量的预处理脚本可能是最好的方法。将代码提交到您的项目中以确保正确性,并将其作为构建脚本的一部分。
A preprocessing script which automatically updates those constants within your code is probably the best method. Commit the code with your project to ensure correctness and have it a part of the build script.
您可以将它们保存在 XML 文档中,并为每种语言编写 XSLT 脚本,以便在每个构建中生成适当的源文件。
You can keep them in a XML document and write XSLT scripts for each language to generate the appropriate source files in each build.
您可以使用 makefile (或等效文件)来定义这些常量吗?对于 C 和 C++。您可以使用编译器的 CLI 选项来定义常量的预处理器值。我没有对 Verilog 进行太多的构建定制,但我怀疑那里也可能存在类似的东西。
Can you use your makefile (or equivalent) to define these constants? For C and C++. you can use the compiler's CLI options to define pre-processor values for the constants. I haven't done much build customization for Verilog, but I suspect something similar might exist there as well.
您可以以以下形式编写一个简单的文件
,然后应用类似的内容,对于 c:
s/\([a-zA-Z][a-zA-Z1-9_]*\)[ \t]* =[ \t]*\(.*\)/#define \1 \2/
值得注意的是,您可能需要指定类型,因为并非所有语言都允许您使用不支持的预处理器宏关心类型。
或者,您可以在 Flex/Bison 中制作词法分析器/解析器来解析配置文件。这样会更清晰,更容易扩展。
You can write a simple file in the form of
and then apply something like, for c:
s/\([a-zA-Z][a-zA-Z1-9_]*\)[ \t]*=[ \t]*\(.*\)/#define \1 \2/
Its worth noting that you might need to specify types because not all languages will allow you to use a preprossessor macro that doesn't care about type.
Alternatively, you can make a lexer/parser in Flex/Bison to parse the configuration file. This will be clearer and easier to extend.
您可能有一个
XML
文件,其中包含要共享的常量,并用每种语言对其进行解析。You might have an
XML
file with the constants to be shared and have it parsed in each language.对于verilog(至少对于系统verilog)和c ++,您可以将所有常量描述为列表(假设所有常量都属于同一类型),如下所示:
在c ++中,您将
在verilog中使用(至少在系统verilog中) )你可以使用这个
我猜 c# 没有包含。因此,您需要一个预处理脚本,至少将您的常量包含在类中。您也许可以使用“cpp”来实现此目的。抱歉,对c#不太了解。
实际上,为了使所有内容相似,我可能会使用 cpp 来生成我需要的文件:
For verilog (at least for system verilog) and c++ you can have all constants described as a list (assuming that all of them are of the same type), like the following:
in c++ you would use
in verilog (at least in system verilog) you can use this
I guess c# does not have includes. So, you would need a pre-pcocessing script there at least to include your constants in a class. You might be able to use 'cpp' for this. Sorry, do not know much about c#.
Actually, to make all similar i would probably use cpp to generate the file i need: