2010-07-15 4 views
5

루아에 예제 lproc 프로그램 (Programming Lua, Chapter 30에 설명되어 있음)을로드하고 어떻게 든 훼손하려고합니다. 나는 이것을 따라 - http://www.lua.org/pil/26.2.html 내 c 모듈을 루아로 가져왔다. 내가 찍은 단계는 다음과 같습니다 :루아에서 C 모듈로드하기

  1. 나는 (정확히 책의 제 30 장에 배치 기능을 포함)을 lproc.h 및 lproc.c 있습니다. 나는 lproc.c를 다음과 같이 컴파일 중이다. --- gcc -c lproc.c -DLUA-USERCONFIG = \ "lproc.h \"

  2. 같은 이름의 lproc.o에서 라이브러리를 만들었다.

  3. 그리고 지시에 따라 lua.c를 컴파일합니다. 내 헤더 파일에는 매크로 LUA_EXTRALIBS 및 메서드 선언이 포함되어 있습니다.

  4. 는 루아 인터프리터에 가서 다음과 같은 오류 준 :

 
> require "lproc" 
stdin:1: module 'lproc' not found: 
    no field package.preload['lproc'] 
    no file './lproc.lua' 
    no file '/opt/local/share/lua/5.1/lproc.lua' 
    no file '/opt/local/share/lua/5.1/lproc/init.lua' 
    no file '/opt/local/lib/lua/5.1/lproc.lua' 
    no file '/opt/local/lib/lua/5.1/lproc/init.lua' 
    no file './lproc.so' 
    no file '/opt/local/lib/lua/5.1/lproc.so' 
    no file '/opt/local/lib/lua/5.1/loadall.so' 
stack traceback: 
    [C]: in function 'require' 
    stdin:1: in main chunk 
    [C]: ? 

모듈이 등록되지 않은 것 같다, 내가 루아에서 무엇을 필요가 있습니까? 시간이 짧고 나는 무언가를 끔찍하게 잘못하고있다. 어떤 방향 으로든 환영 할 만하다.

감사합니다,
사얀

+0

어떤 버전의 루아를 사용하고 있습니까? 온라인 PIL은 구식입니다 –

+0

Macport에서 Lua 5.1.4를 다운로드했습니다. – Sayan

답변

1

은 루아를위한 C 라이브러리 (모든 플랫폼, 루아 5.1-5.3 및 LuaJIT에서 작동) 구축의 완전하고 완벽하게 휴대 최소한의 예는 다음과 같습니다

example.c이 가진 : example-1.0-1.rockspec라는 이름의 같은 디렉토리에이 rockspec 파일을 넣어

#include <lua.h> 

int example_hello(lua_State* L) { 
    lua_pushliteral(L, "Hello, world!"); 
    return 1; 
} 

int luaopen_example(lua_State* L) { 
    lua_newtable(L); 
    lua_pushcfunction(L, example_hello); 
    lua_setfield(L, -2, "hello"); 
    return 1; 
} 

:

package = "example" 
version = "1.0-1" 
source = { 
    url = "." -- not online yet! 
} 
build = { 
    type = "builtin", 
    modules = { 
     example = "example.c" 
    } 
} 

그런 다음 luarocks make을 실행하십시오. 플랫폼에 맞는 플래그로 C 코드를 빌드합니다.

이제 모듈을 사용할 준비가되었습니다.

Lua 5.3.3 Copyright (C) 1994-2016 Lua.org, PUC-Rio 
> example = require("example") 
> print(example.hello()) 
Hello, world! 
> 
관련 문제