从C ++ / C设置全局变量LUA_PATH? [英] Setting the global LUA_PATH variable from C++/C?

查看:1135
本文介绍了从C ++ / C设置全局变量LUA_PATH?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从C / C ++直接设置我的全局变量LUA_PATH,我使用的Lua从我的iPhone应用程序,所以我的路往往不应用程序之间的变化(每个iPhone应用程序在设备中的单独的文件夹)。

I'm trying to set my global LUA_PATH variable directly from C/C++, I'm using Lua from my iPhone applications, so my path tends does change between applications ( each iPhone app has a separate folder in the device ).

我知道我可以用一个固定路径重新编译LUA设置LUA_PATH,但这是相当很不理想。

I know I could set the LUA_PATH by recompiling lua with a "fixed" path, but that's quite far from ideal.

(我想这样做是为了能够使用要求,从我的 .lua 脚本。

( I'm trying to do this in order to be able to use require, from my .lua scripts.

谁能帮我在这里?

推荐答案

在C ++:

int setLuaPath( lua_State* L, const char* path )
{
    lua_getglobal( L, "package" );
    lua_getfield( L, -1, "path" ); // get field "path" from table at top of stack (-1)
    std::string cur_path = lua_tostring( L, -1 ); // grab path string from top of stack
    cur_path.append( ";" ); // do your path magic here
    cur_path.append( path );
    lua_pop( L, 1 ); // get rid of the string on the stack we just pushed on line 5
    lua_pushstring( L, cur_path.c_str() ); // push the new one
    lua_setfield( L, -2, "path" ); // set the field "path" in table at -2 with value at top of stack
    lua_pop( L, 1 ); // get rid of package table from top of stack
    return 0; // all done!
}

我没有测试或编译它。我用: http://lua.org/pil 并的 http://lua.org/manual/5.1

这篇关于从C ++ / C设置全局变量LUA_PATH?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆