跨语言共享常量

发布于 2024-09-15 19:39:19 字数 140 浏览 8 评论 0原文

我有一长串常量,需要在不同语言(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一腔孤↑勇 2024-09-22 19:39:19

自动更新代码中这些常量的预处理脚本可能是最好的方法。将代码提交到您的项目中以确保正确性,并将其作为构建脚本的一部分。

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.

淡忘如思 2024-09-22 19:39:19

您可以将它们保存在 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.

一百个冬季 2024-09-22 19:39:19

您可以使用 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.

森末i 2024-09-22 19:39:19

您可以以以下形式编写一个简单的文件

const1 = value1
const2 = value2
const3 = value3

,然后应用类似的内容,对于 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

const1 = value1
const2 = value2
const3 = value3

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.

指尖微凉心微凉 2024-09-22 19:39:19

您可能有一个 XML 文件,其中包含要共享的常量,并用每种语言对其进行解析。

You might have an XML file with the constants to be shared and have it parsed in each language.

旧街凉风 2024-09-22 19:39:19

对于verilog(至少对于系统verilog)和c ++,您可以将所有常量描述为列表(假设所有常量都属于同一类型),如下所示:

a=0, b= 1, c = 2, ..;

在c ++中,您将

const int
#include <myconsts>

在verilog中使用(至少在系统verilog中) )你可以使用这个

parameter int
`include "myconsts"

我猜 c# 没有包含。因此,您需要一个预处理脚本,至少将您的常量包含在类中。您也许可以使用“cpp”来实现此目的。抱歉,对c#不太了解。

实际上,为了使所有内容相似,我可能会使用 cpp 来生成我需要的文件:

#ifdef CPP
    const int
#elsif VERILOG
    parameter int
#elsif CSHARP
    class Constants {
       const int
#endif

    a = 0,
    c = 1,
    d = 2;

#ifdef(CSHARP)
    };
#endif

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:

a=0, b= 1, c = 2, ..;

in c++ you would use

const int
#include <myconsts>

in verilog (at least in system verilog) you can use this

parameter int
`include "myconsts"

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:

#ifdef CPP
    const int
#elsif VERILOG
    parameter int
#elsif CSHARP
    class Constants {
       const int
#endif

    a = 0,
    c = 1,
    d = 2;

#ifdef(CSHARP)
    };
#endif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文