我有一个像以下代码一样的工作项目,运行在Visual Studio2013,Windows7N上。我试着用luaL_loadbuffer(L,s,strlen(s),name)替换luaL_loadfile(),这样我就可以把脚本作为字符串放在main中,因为在我的其他项目中,打开项目中的文件有问题,但我设法直接调用lua脚本,将脚本作为字符串放在main()中。我的问题是:这个luaL_loadbuffer()是如何工作的?我的意思是,如果我正确理解这个函数,luaL_loadbuffer(L,s,strlen(s),name),"s“表示字符串。我试着用luaL_loadbuffer()调试,但是不能通过调试,总是得到错误status= 2。此外,我还看到其他人使用luaL_loadbuffer()加载脚本文件,所以我现在很困惑。有谁可以帮我?
-- last.lua
function f ()
print("Hello from Lua")
end
#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
double z;
int error;
lua_State *L = luaL_newstate();
luaL_openlibs(L);
if (luaL_loadfile(L, "last.lua") || lua_pcall(L, 0, 0, 0))
{
printf("error: %s", lua_tostring(L, -1));
return -1;
}
lua_getglobal(L, "f");
if (!lua_isfunction(L, -1))
{
lua_pop(L, 1);
return -1;
}
if (lua_pcall(L, 0, 0, 0) != 0)
{
printf("error running function `f': %s\n", lua_tostring(L, -1));
return -1;
}
lua_close(L);
return 0;
}https://stackoverflow.com/questions/34880889
复制相似问题