The Erl_Interface documentation code appears not to work

I’m trying to learn how Erl_Interface works but all the code written on the documentation appears not to work, someone could explain me why?? Below I explain you how to exactly reproduce the problem.

Environment configuration

In order to have the same environment of mine you can you use nix and open a shell in this way:

nix-shell -p erlang gcc

I strongly suggest you to use nix but if you don’t want it, versions of the program I’m using are:

  • erl => 24.2
  • gcc => 11.3.0

C code

Create a main.c file with this code:

#include "ei.h"

int main(int argc, char const *argv[])
{
    // PARAGRAPH 1.3 OF https://www.erlang.org/doc/apps/erl_interface/ei_users_guide.html
    ei_init();
    // PARAGRAPH 1.5 OF https://www.erlang.org/doc/apps/erl_interface/ei_users_guide.html
    ei_x_buff buf;
    int i = 0;
    ei_x_new(&buf);
    ei_x_format_wo_ver(&buf,
                       "[{name,~a},{age,~i},{data,[{adr,~s,~i}]}]",
                       "madonna",
                       21,
                       "E-street", 42);
    ei_print_term(stdout, buf.buff, &i);
    ei_x_free(&buf);
    return 0;
}

Note that the code is exactly copied from here!

Compile the example

Then you should compile the example with these shell commands (taken from 1.3):

# TO GET THE PATH OF ERL_INTERFACE LIB
export ERL_INTERFACE=`erl -noshell -eval 'io:format(code:root_dir())' -eval 'init:stop()'`/lib/erl_interface-5.1
cc -c -I$ERL_INTERFACE/include main.c
ld -L$ERL_INTERFACE/lib main.o -lei -o main

but after executing ld a long error message is produced, all the messages say that some references are missing but I didn’t know how to fix it.

Error message is something like that (I don’t attach all the message because is pretty long and the messages are quite similar, they are all about to some missing references)

/nix/store/jfkmqz20q8fsaqizcml86adxqyzlwllx-binutils-2.38/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000401000
/nix/store/jfkmqz20q8fsaqizcml86adxqyzlwllx-binutils-2.38/bin/ld: main.o: in function `main':
main.c:(.text.startup+0x66): undefined reference to `stdout'
/nix/store/jfkmqz20q8fsaqizcml86adxqyzlwllx-binutils-2.38/bin/ld: main.c:(.text.startup+0x93): undefined reference to `__stack_chk_fail'
/nix/store/jfkmqz20q8fsaqizcml86adxqyzlwllx-binutils-2.38/bin/ld: /nix/store/922azh3v681zsgr1fd2xrg3480578kjb-erlang-24.2/lib/erlang/lib/erl_interface-5.1/lib/libei.a(ei_format.o): in function `pdigit':
(.text+0x208): undefined reference to `__ctype_b_loc'
/nix/store/jfkmqz20q8fsaqizcml86adxqyzlwllx-binutils-2.38/bin/ld: (.text+0x245): undefined reference to `__isoc99_sscanf'
/nix/store/jfkmqz20q8fsaqizcml86adxqyzlwllx-binutils-2.38/bin/ld: (.text+0x295): undefined reference to `__isoc99_sscanf'
/nix/store/jfkmqz20q8fsaqizcml86adxqyzlwllx-binutils-2.38/bin/ld: (.text+0x2a8): undefined reference to `__stack_chk_fail'
/nix/store/jfkmqz20q8fsaqizcml86adxqyzlwllx-binutils-2.38/bin/ld: /nix/store/922azh3v681zsgr1fd2xrg3480578kjb-erlang-24.2/lib/erlang/lib/erl_interface-5.1/lib/libei.a(ei_format.o): in function `eiformat':
(.text+0x2de): undefined reference to `__ctype_b_loc'
/nix/store/jfkmqz20q8fsaqizcml86adxqyzlwllx-binutils-2.38/bin/ld: (.text+0x646): undefined reference to `__stack_chk_fail'
/nix/store/jfkmqz20q8fsaqizcml86adxqyzlwllx-binutils-2.38/bin/ld: /nix/store/922azh3v681zsgr1fd2xrg3480578kjb-erlang-24.2/lib/erlang/lib/erl_interface-5.1/lib/libei.a(ei_format.o): in function `plist':
(.text+0x6a3): undefined reference to `__ctype_b_loc'
/nix/store/jfkmqz20q8fsaqizcml86adxqyzlwllx-binutils-2.38/bin/ld: (.text+0x80f): undefined reference to `__stack_chk_fail'
/nix/store/jfkmqz20q8fsaqizcml86adxqyzlwllx-binutils-2.38/bin/ld: /nix/store/922azh3v681zsgr1fd2xrg3480578kjb-erlang-24.2/lib/erlang/lib/erl_interface-5.1/lib/libei.a(ei_format.o): in function `ptuple':
(.text+0x860): undefined reference to `__ctype_b_loc'
/nix/store/jfkmqz20q8fsaqizcml86adxqyzlwllx-binutils-2.38/bin/ld: (.text+0x93b): undefined reference to `__stack_chk_fail'
/nix/store/jfkmqz20q8fsaqizcml86adxqyzlwllx-binutils-2.38/bin/ld: /nix/store/922azh3v681zsgr1fd2xrg3480578kjb-erlang-24.2/lib/erlang/lib/erl_interface-5.1/lib/libei.a(ei_format.o): in function `ei_x_format':
(.text+0xa53): undefined reference to `__stack_chk_fail'
/nix/store/jfkmqz20q8fsaqizcml86adxqyzlwllx-binutils-2.38/bin/ld: /nix/store/922azh3v681zsgr1fd2xrg3480578kjb-erlang-24.2/lib/erlang/lib/erl_interface-5.1/lib/libei.a(ei_format.o): in function `ei_x_format_wo_ver':
(.text+0xb69): undefined reference to `__stack_chk_fail'
/nix/store/jfkmqz20q8fsaqizcml86adxqyzlwllx-binutils-2.38/bin/ld: /nix/store/922azh3v681zsgr1fd2xrg3480578kjb-erlang-24.2/lib/erlang/lib/erl_interface-5.1/lib/libei.a(ei_malloc.o): in function `ei_malloc':
(.text+0x1): undefined reference to `malloc'
/nix/store/jfkmqz20q8fsaqizcml86adxqyzlwllx-binutils-2.38/bin/ld: /nix/store/922azh3v681zsgr1fd2xrg3480578kjb-erlang-24.2/lib/erlang/lib/erl_interface-5.1/lib/libei.a(ei_malloc.o): in function `ei_realloc':
(.text+0x11): undefined reference to `realloc'
/nix/store/jfkmqz20q8fsaqizcml86adxqyzlwllx-binutils-2.38/bin/ld: /nix/store/922azh3v681zsgr1fd2xrg3480578kjb-erlang-24.2/lib/erlang/lib/erl_interface-5.1/lib/libei.a(ei_malloc.o): in function `ei_free':
(.text+0x21): undefined reference to `free'
/nix/store/jfkmqz20q8fsaqizcml86adxqyzlwllx-binutils-2.38/bin/ld: /nix/store/922azh3v681zsgr1fd2xrg3480578kjb-erlang-24.2/lib/erlang/lib/erl_interface-5.1/lib/libei.a(ei_printterm.o): in function `xprintf':
(.text+0xb8): undefined reference to `__vfprintf_chk'

Plese someone could help me???

I just followed what documentation says and I get an error as a result. I’m doing something wrong or the documentation is wrong and it should be updated?

2 Likes

I have zero experience with erl_interface or nix, but the example about linking executables with ld is just plain wrong. Unless you know exactly what you’re doing and how everything fits together, you should use your compiler to link your executables, not ld. In this case that would be:

cc -o main main.o -L$ERL_INTERFACE/lib -lei

For starters, the ld invocation totally fails to include libc or C start-up files, or selecting the correct linker map. cc does handle these things.

4 Likes

Thanks for your answer that helps me a lot. So documentation is wrong, it should be updated!

1 Like

Please open an issue at Issues · erlang/otp · GitHub for this :wink:

1 Like

erl_interface is a library that implements most of what you did in your C external program, it’s like C standard Library for the most important C headers like <stdio.h>, if not linked properly that means the C program will not recognize most of predefined functions and types and that what caused your errors