2012-09-12 2 views
1

this question에 설명 된대로 인쇄 기능을 재정의하려고합니다. 여기 내 코드는 다음과 같습니다루아에서 인쇄 기능을 재정의하는 데 문제가 있습니다

extern "C"{ 
#include <lua.h> 
#include <lauxlib.h> 
#include <lualib.h> 
} 

#include <iostream> 

using namespace std; 

lua_State* L; 

static int l_my_print(lua_State* L) { 
    int nargs = lua_gettop(L); 

    for (int i=1; i <= nargs; i++) { 
     if (lua_isstring(L, i)) { 
      cout << "!!!" << lua_tostring(L, i) << "!!!" << endl; 
     } 
    } 

    return 0; 
} 

static const struct luaL_Reg printlib [] = { 
    {"print", l_my_print}, 
    {NULL, NULL} /* end of array */ 
}; 

extern int luaopen_luamylib(lua_State *L) 
{ 
    lua_getglobal(L, "_G"); 
    luaL_register(L, NULL, printlib); 
    lua_pop(L, 1); 
} 


int main(){ 
    L = luaL_newstate(); 
    luaL_openlibs(L); 
    luaopen_luamylib(L); 

    luaL_dostring(L, "print(\"hello\")"); 

    lua_close(L); 

    return 0; 
} 

내가 코드를 컴파일 할 때, 내가 얻을 :

$ g++ -I/usr/include/lua5.2 -o embed test.cpp -Wall -Wextra -llua5.2 
test.cpp:28:1: error: elements of array ‘const luaL_reg printlib []’ have incomplete type 
test.cpp:28:1: error: storage size of ‘printlib’ isn’t known 
test.cpp: In function ‘int luaopen_luamylib(lua_State*)’: 
test.cpp:33:34: error: ‘luaL_register’ was not declared in this scope 
test.cpp:35:1: warning: no return statement in function returning non-void [-Wreturn-type] 

사람이 여기에 발생하는 것을 설명 할 수 있습니까? 도서관 같은 거 없어?

UPDATE는

이 구조체는 luaL_reg, luaL_Reg을하지라고 지적했다.

$ g++ -I/usr/include/lua5.2 -o embed test.cpp -Wall -Wextra -llua5.2 
test.cpp: In function ‘int luaopen_luamylib(lua_State*)’: 
test.cpp:33:34: error: ‘luaL_register’ was not declared in this scope 
test.cpp:35:1: warning: no return statement in function returning non-void [-Wreturn-type] 
+0

'struct luaL_reg'를 정의하는 헤더가 누락 된 것처럼 처음 두 개의 오류가 들립니다. –

답변

4

첫 번째 오류 : 그것은 luaL_Reg하지 luaL_reg입니다 이것은 내 첫 번째 문제를 해결했다.

두 번째 오류 : luaL_register는 (Lua 5.2에서는) 더 이상 사용되지 않으며 Lua 헤더를 포함하기 전에 LUA_COMPAT_MODULE이 정의 된 경우에만 사용할 수 있습니다. 대신 luaL_setfuncs를 사용해야합니다.

+0

첫 번째 오류가 수정되었습니다. 두 번째는 어때? – ewok

관련 문제