2013-10-09 4 views
1

얼랭 (Erlang) 드라이버의 작동 방식을 더 잘 이해하기 위해 책에서 간단한 예제로 시작했지만 원시 얼랭 (Erlang) 드라이버 코드가 포함 된 C 파일을 컴파일하려고하면 다음과 같은 컴파일 오류 메시지가 나타납니다.Erlang 드라이버를 컴파일하는 방법은 무엇입니까?

/tmp/ccQ0GroH.o:example1_lid.c:(.text+0xe) driver_alloc' /tmp/ccQ0GroH.o:example1_lid.c:(.text+0x2f): undefined reference to driver_free ' /tmp/ccQ0GroH.o:example1_lid.c:(.text+0xb0)에 정의되지 않은 참조 :'로 정의 참조 driver_output '

왜 이런 일이 벌어지고 어떻게 해결할 수 있는지 아는 사람이 있습니까? 참조 용으로 C 파일이 아래에 게시됩니다.

감사합니다.

/* example1_lid.c */ 

#include <stdio.h> 
#include "erl_driver.h" 

typedef struct { 
    ErlDrvPort port; 
} example_data; 

static ErlDrvData example_drv_start(ErlDrvPort port, char *buff) 
{ 
    example_data* d = (example_data*)driver_alloc(sizeof(example_data)); 
    d->port = port; 
    return (ErlDrvData)d; 
} 

static void example_drv_stop(ErlDrvData handle) 
{ 
    driver_free((char*)handle); 
} 

static void example_drv_output(ErlDrvData handle, char *buff, int bufflen) 
{ 
    example_data* d = (example_data*)handle; 
    char fn = buff[0], arg = buff[1], res; 
    if (fn == 1) { 
     res = twice(arg); 
    } else if (fn == 2) { 
     res = sum(buff[1], buff[2]); 
    } 
    driver_output(d->port, &res, 1); 
} 

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(example_drv) /* must match name in driver_entry */ 
{ 
    return &example_driver_entry; 
} 
+0

컴파일 플래그를 제공 할 수 있습니까? – user

답변

1

귀하의 코드는 링크 된 드라이버를 작성하려고한다는 것을 의미합니다. 이러한 드라이버는 documented과 같은 공유 라이브러리로 컴파일해야합니다. gcc를 사용하는 경우 -shared-fpic을 전달해야합니다. 링커에서 정의되지 않은 참조 오류가 발생한다는 사실은 사용자가 공유 라이브러리를 작성하려고하지 않는다는 것을 암시합니다. 책에서

  1. 예는 매우 오래된 :

    여기에 두 개 이상의 추가적인 문제가 있습니다. Erlang의 같은 버전을 사용하고 있다면 괜찮습니다. 최신 릴리스를 사용하는 경우 the documentation을 참조하십시오. 특히 ErlDrvEntry 구조가 너무 작아서 다른 많은 필드가 포함되어 있습니다.

  2. DRIVER_INIT 매크로는 comment에 언급 된 driver_entry의 이름과 일치하는 인수를 취해야합니다. 그러나 코드의 경우는 그렇지 않습니다. 이름은 "example1_drv"이고 매크로는 example_drv으로 호출됩니다. 또한

, driver_free 지금은 (?) void* 소요 캐스트는 불필요하다.

관련 문제