我有一个使用 Lua 5.2.1 的 Visual Studio 2008 C++03 应用程序。我想用一个名为“foo”的模块扩展Lua,但是当我调用require("foo")
在我的 Lua 脚本中,我收到错误:
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\
我的Lua脚本:
foo.bar()
我的 lua_foo.h 文件:
#include <lua.h>
extern "C" int luaopen_foo( lua_State* L );
我的 lua_foo.cpp 文件:
#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;
}
它们被编译到静态库“lua_foo.lib”中,该库静态链接到我的主 Lua 可执行文件。
有人能帮助我理解我哪里出了问题吗?谢谢。我宁愿避免使用 c++ 包装器(暂时),并且我不想将此库打包为与主 Lua 引擎分开的 DLL。
EDIT
问题出在 lua 引擎代码中。我添加了luaL_requiref
根据@NicolBolas 的建议。
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 );
}