保持 Erlang 和 C 之间的定义同步

发布于 2024-10-02 23:42:53 字数 403 浏览 3 评论 0原文

来自 http://www.erlang.org/doc/apps/erts/driver。 html

/* Keep the following definitions in alignment with the
 * defines in erl_pq_sync.erl
 */
#define DRV_CONNECT             'C'
#define DRV_DISCONNECT          'D'
#define DRV_SELECT              'S'

是否有任何简单的方法可以在 Erlang 和 C 源之间共享宏的值?

From http://www.erlang.org/doc/apps/erts/driver.html:

/* Keep the following definitions in alignment with the
 * defines in erl_pq_sync.erl
 */
#define DRV_CONNECT             'C'
#define DRV_DISCONNECT          'D'
#define DRV_SELECT              'S'

Is there any simple way to share the values of macros between Erlang and C sources?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

扮仙女 2024-10-09 23:42:53

我对 Erlang 一无所知,但想必你不能只创建一个 .h 文件,其中只包含所需的定义并在两个地方 #include 它(或等效的)。

假设你不能做到这一点,我会考虑自动生成一个文件。

编辑:刚刚查看了 Erlang 文档,格式非常相似,但不完全相同。

Erlang 需要 -define(Const, Replacement)

C 需要 #define const replacement

所以我会编写一个包含 Erlang 语法的文本文件(仅用于这些定义),然后 执行一些操作,然后将其#include 添加到我的 C 源代码中。

sed s/-define/#define/g 
sed s/[\(\),]//g

作为 C 构建中的预构建步骤,我会在该文件的临时副本上

I know nothing about Erlang, but presumably you can't just create a .h file with just the required defines in and #include it (or equivalent) in both places.

Asuming you can't do this, I would look at auto generating a file for one from the other.

EDIT: Having just looked at the Erlang docs, the format is very similar but not quite the same.

Erlang needs -define(Const, Replacement)

C needs #define const replacement

So I would write a single text file which contained the Erlang syntax (for just these definitions) and then as a pre-build step in my C build I would do something along the lines of

sed s/-define/#define/g 
sed s/[\(\),]//g

on a temporary copy of that file, which I would then #include in my C source.

情深缘浅 2024-10-09 23:42:53

您可以(尝试)在erlang中使用gcc的C预处理器,因为gcc有选项:

  • -E stop after preprocessing stage
  • -x language (您可以设置一个给出正确的值)输出)
  • -P 禁止输出 #line
  • -C 保留注释(不要删除 /* */ 和 // )

You may (try to) use C preprocessor of gcc in erlang, as gcc have options:

  • -E stop after preprocessing stage
  • -x language (you may set one which gives correct output)
  • -P inhibit output of #line
  • -C keep comments (do not remove /* */ and // )
七度光 2024-10-09 23:42:53

一种非常动态的方法是在 C 中保留一个表,该表可以通过使用具有 char *name -> 的 # 的宏轻松生成。价值观。

然后用它在开始时向 erlang 发送一个表。

#define DRV_CONNECT             'C'
#define DRV_DISCONNECT          'D'
#define DRV_SELECT              'S'

#define ENTRY(X) {#X, X}

struct table_entry
{
    const char *name;
    int value
};

struct table_entry table[] =
{
   ENTRY(DVR_CONNECT),
   ENTRY(DRV_DISCONNECT),
   ENTRY(DRV_SELECT)
};

使用此表将其开头发送到 erlang,将其解码为元组列表并使用它进行查找。

One very dynamic way is to keep a table in C wich can easily generated by macros using # that have char *name -> values.

Then use this to send erlang a table at the beginning.

#define DRV_CONNECT             'C'
#define DRV_DISCONNECT          'D'
#define DRV_SELECT              'S'

#define ENTRY(X) {#X, X}

struct table_entry
{
    const char *name;
    int value
};

struct table_entry table[] =
{
   ENTRY(DVR_CONNECT),
   ENTRY(DRV_DISCONNECT),
   ENTRY(DRV_SELECT)
};

Use this table to send it at the beginning to erlang, decode it there into a tuple list and use this to look up.

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