从 C++/C 设置全局 LUA_PATH 变量?

2023-09-27C/C++开发问题
0

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

问题描述

我试图直接从 C/C++ 设置我的全局 LUA_PATH 变量,我在我的 iPhone 应用程序中使用 Lua,所以我的路径往往会在应用程序之间发生变化(每个 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 脚本中使用 require.

( 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

I haven't tested or compiled it. I used: http://lua.org/pil and http://lua.org/manual/5.1

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

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6