Erlang interoperability

在HTML文档的doc/tutorial下有文件(新添Makefile):

inter
|-- Makefile
|-- cnode_c.c
|-- cnode_s.c
|-- cnode_s2.c
|-- complex.c
|-- complex1.erl
|-- complex2.erl
|-- complex3.erl
|-- complex4.erl
|-- complex5.erl
|-- ei.c
|-- erl_comm.c
|-- port.c
`-- port_driver.c
CFLAGS=-I/usr/local/lib/erlang/lib/erl_interface-3.6.4/include -I/usr/local/lib/erlang/erts-5.7.4/include
LDFLAGS=-L/usr/local/lib/erlang/lib/erl_interface-3.6.4/lib

APPS=cserver cserver2 cclient ext_ei ext_port example_drv.so

all: $(APPS)

cserver: complex.o cnode_s.o
	gcc $(LDFLAGS) $^ -lerl_interface -lei -lpthread -o $@

cserver2: complex.o cnode_s2.o
	gcc $(LDFLAGS) $^ -lerl_interface -lei -lpthread -o $@

cclient: complex.o cnode_c.o
	gcc $(LDFLAGS) $^ -lerl_interface -lei -lpthread -o $@

ext_port: complex.o erl_comm.o port.o
	gcc $(LDFLAGS) $^ -lerl_interface -lei -lpthread -o $@

ext_ei: complex.o erl_comm.o ei.o
	gcc $(LDFLAGS) $^ -lerl_interface -lei -lpthread -o $@

example_drv.so: complex.o port_driver.o
	gcc -o $@ -fpic -shared $^

%.o : %.c
	gcc -c $(CFLAGS) $^

clean:
	rm -rf *.beam
	rm -rf *.o
	rm -rf $(APPS)

ProgrammingErlang(JoeArmstrong)随书的代码有误,编译的.so无法加载:

ErlDrvEntry example_driver_entry = {
    NULL,               /* F_PTR init, N/A */
    example_drv_start,  /* L_PTR start, called when port is opened */
    example_drv_stop,   /* F_PTR stop, called when port is closed */
    example_drv_output, /* F_PTR output, called when erlang has sent
			   data to the port */
    NULL,               /* F_PTR ready_input, 
                           called when input descriptor ready to read*/
    NULL,               /* F_PTR ready_output, 
                           called when output descriptor ready to write */
    "example1_drv",     /* char *driver_name, the argument to open_port */
    NULL,               /* F_PTR finish, called when unloaded */
    NULL,               /* F_PTR control, port_command callback */
    NULL,               /* F_PTR timeout, reserved */
    NULL                /* F_PTR outputv, reserved */
};

DRIVER_INIT(example1_drv) /* must match name in driver_entry */
{
    return &example_driver_entry;
}

DRIVER_INIT(example1_drv)/*mustmatchnameindriver_entry*/

带的erl_driver.h删除,用系统的。

相关推荐