我想直接从C/C++设置我的全局LUA_PATH变量,我从我的iPhone应用程序使用Lua,所以我的路径往往会在应用程序(每个iPhone应用程序在设备中有一个单独的文件夹)之间发生更改。从C++/C设置全局LUA_PATH变量?
我知道我可以通过用“固定”路径重新编译lua来设置LUA_PATH,但这非常不理想。
(我想这样做是为了能够使用require
,从我.lua
脚本。
谁能帮我在这里?
我想直接从C/C++设置我的全局LUA_PATH变量,我从我的iPhone应用程序使用Lua,所以我的路径往往会在应用程序(每个iPhone应用程序在设备中有一个单独的文件夹)之间发生更改。从C++/C设置全局LUA_PATH变量?
我知道我可以通过用“固定”路径重新编译lua来设置LUA_PATH,但这非常不理想。
(我想这样做是为了能够使用require
,从我.lua
脚本。
谁能帮我在这里?
在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
我想这不会是因为候选条件,如你所提及般,出于安全原因,每个iPhone应用程序住在它自己的沙盒。
我想用setenv
将设置环境变量只对当前进程和它的孩子。
顺便说一下:如果计划将您的应用程序提交到AppStore,(据我所知),脚本语言/口译人员会被您签署的合同所吸引。
#include <stdlib.h>
...
setenv ("LUA_PATH", (char *)my_path, 1);
...或者类似的东西...
我不确定它是否适用于iPhone。 – onemasse 2010-11-08 17:33:44
您也可以致电require
前更改package.path
在Lua。
我对iPhone开发不太熟悉,但是在执行应用程序之前可以设置LUA_PATH env变量吗?
例如,在Linux中,我可以写,执行的二进制文件,像这样的脚本:
export LUA_PATH="foo"
/path/to/executable
的Windows与批处理文件类似的功能。
如果你真的需要在代码中进行更改,我不知道如何使用luaL_loadbuffer和lua_pcall来执行“package.path = blah”命令。
或'env LUA_PATH =“foo”/ path/to/executable'。 – lhf 2010-11-08 20:38:06
我不认为LUA_PATH是一个环境变量。 – Watusimoto 2013-11-10 23:43:01
@Watusimoto http://www.lua.org/pil/8.1.html。 Grep LUA_PATH。 – lhumongous 2013-11-11 16:19:53
ObjC:从另一个答案,以下是对我有用。需要添加“/?.lua”。
int setLuaPath(NSString* path) { lua_getglobal(L, "package"); lua_getfield(L, -1, "path"); // get field "path" from table at top of stack (-1) NSString * cur_path = [NSString stringWithUTF8String:lua_tostring(L, -1)]; // grab path string from top of stack cur_path = [cur_path stringByAppendingString:@";"]; // do your path magic here cur_path = [cur_path stringByAppendingString:path]; cur_path = [cur_path stringByAppendingString:@"/?.lua"]; lua_pop(L, 1); // get rid of the string on the stack we just pushed on line 5 lua_pushstring(L, [cur_path UTF8String]); // 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! } ... add this code somewhere, near where you lua_open() for example // Set Lua's Package.path to where our Lua files can be found NSString *luaPath = [[NSBundle mainBundle] pathForResource:@"name of any one of my lua files" ofType:@"lua"]; setLuaPath([luaPath stringByDeletingLastPathComponent]); ...
这工作完美无缺,谢谢。正是我在找什么。 – codeshaman 2012-12-21 00:25:50
通过执行一对lual_dostring函数,可以很容易地在C++中设置LUA_PATH和LUA_CPATH。
luaL_dostring(L, "package.path = package.path .. ';?.lua'");
luaL_dostring(L, "package.cpath = package.cpath .. ';?.dll'");
这两条线段叫你有你的lua_State(L这里)和你的lual_loadfile()之前和之后lau_pcall()函数调用将增加当前设置的路径,并则将cpath。在这种情况下,我添加了一条指令,以执行本地执行。
花了我几小时才找到这个解决方案..我希望它有帮助。
我相信他们曾经被禁止,但已经不再。 – Puppy 2010-11-08 17:40:34
@ DeadMG2也许我错过了什么。我认为没有人详细阅读合同的频繁更改和增加 – Kai 2010-11-08 17:43:22