Wie erweitere ich Lua mit einer statischen C ++ - Bibliothek?
Ich habe eine Visual Studio 2008 C ++ 03-Anwendung, die Lua 5.2.1 verwendet. Ich möchte Lua mit einem Modul namens "foo" erweitern, aber wenn ich anruferequire("foo")
In meinem Lua-Skript erhalte ich den Fehler:
foo_test.lua:1: module 'foo' not found:
no field package.preload['process']
no file '!\lua\process.lua'
no file '!\lua\process\init.lua'
no file '!\process.lua'
no file '!\process\
Mein Lua-Skript:
foo.bar()
Meine lua_foo.h Datei:
#include <lua.h>
extern "C" int luaopen_foo( lua_State* L );
Meine lua_foo.cpp-Datei:
#include "lua_foo.h"
#include <lua.hpp>
static int l_bar( lua_State *L )
{
puts( "in bar()" );
return 1;
}
int luaopen_foo( lua_State *L )
{
static const luaL_Reg foo[] = {
{ "bar", l_bar },
{ NULL, NULL }
};
luaL_newlib( L, foo );
return 1;
}
Diese werden in einer statischen Bibliothek "lua_foo.lib" kompiliert, die statisch mit meiner ausführbaren Lua-Hauptdatei verknüpft ist.
Kann mir jemand helfen zu verstehen, wo ich falsch liege? Vielen Dank. Ich würde es vorziehen, C ++ - Wrapper (vorerst) zu vermeiden, und ich möchte diese Bibliothek nicht als separate DLL von der Haupt-Lua-Engine packen.
BEARBEITEN
Das Problem lag im lua-Motorcode. Ich fügte hinzu, dieluaL_requiref
per @NicolBolas Vorschlag.
lua_State* L = luaL_newstate();
if( NULL != L )
{
luaL_openlibs( L );
luaL_requiref( token.get(), "foo", luaopen_foo, 1 );
luaL_dofile( L, "foo_test.lua" );
lua_close( L );
}