Alexey Romanov
Alexey Romanov

Reputation: 170835

Keeping defines in sync between Erlang and 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?

Upvotes: 1

Views: 119

Answers (3)

Peer Stritzinger
Peer Stritzinger

Reputation: 8372

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.

Upvotes: 1

Vovanium
Vovanium

Reputation: 3898

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 // )

Upvotes: 1

Vicky
Vicky

Reputation: 13244

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.

Upvotes: 3

Related Questions