1
我已經測試下面的C++代碼:編譯模塊LUA用gcc
#include <lua.hpp>
#include <iostream>
static int dummy(lua_State * L)
{
std::cout << "Test";
return 0;
}
int luaopen_testlib(lua_State * L)
{
lua_register(L,"dummy",dummy);
return 0;
}
我用命令編譯它,這讓我沒有任何錯誤:
g++ -Wextra -O2 -c -o testlib.o main.cpp
g++ -shared -o testlib.so testlib.o
但是,當我嘗試加載它在lua我得到未定義的符號錯誤爲:
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
> require"testlib"
error loading module 'testlib' from file './testlib.so':
./testlib.so: undefined symbol: _Z16lua_pushcclosureP9lua_StatePFiS0_Ei
在我看來g ++命令中缺少某些東西,但我一直在尋找整個上午的解決方案,無法得到這個簡單的例子來編譯。
編輯:
數重新編譯後,它返回:
error loading module 'testlib' from file './testlib.so':
./testlib.so: undefined symbol: luaopen_testlib
這是通過將解決:
extern "C"
{
int luaopen_testlib(lua_State *L)
{
lua_register(L,"dummy",dummy);
return 0;
}
}
我不知道它是否可能是一個名稱mangling的問題......你是用與Lua相同的編譯器構建你的代碼嗎? – nneonneo
lua和gcc都來自archlinux回購站。歐,我試着從http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/簡單的例子,它工作得很好。 – UldisK
[我如何用靜態C++庫擴展Lua?](http://stackoverflow.com/questions/12058186/how-do-i-extend-lua-with-a-static-c-library) –