为什么C++看不到我的Lua脚本文件?

我在将 Lua 集成到 C++ 中遇到了一些问题。

我的项目在 Visual Studio 中,我运行了 my lua_init() 函数:

bool lua_init(std::string &luaScriptName){
// 创建 Lua 状态。来自 Lua 5.1 参考手册的解释:

globalL = luaL_newstate(); // 在 5.1 版本中正确 - Lua 5.0 或更低版本可能需要不同的命令

if( globalL == NULL )
    return false;

    // 加载所有 Lua 标准库
luaL_openlibs(globalL);  // 在 5.1 版本中 OK - 在 Lua 5.0 或更低版本中需要不同的命令

// 做下面的事情可以用 luaL_dofile 替换,但是那只是:luaL_loadfile(..) || lua_pcall(..),没有报告错误消息等。
int initError = luaL_loadfile(globalL,luaScriptName.c_str());
switch( initError )
{
    case 0:
        // 文件加载 OK,因此调用它作为受保护的函数 - 以防止致命错误导致退出程序
        lua_pcall(globalL,0,0,0);
        break;
    case LUA_ERRFILE:
        std::cerr<<"无法找到 / 打开 Lua 脚本文件:" << luaScriptName << std::endl <<"跳过 Lua 初始化。" << std::endl;
        break;
    case LUA_ERRSYNTAX:
        std::cerr<<"脚本文件预编译期间语法错误:" << luaScriptName << std::endl <<"跳过 Lua 初始化。" << std::endl;
        break;
    case LUA_ERRMEM:
        // 将此视为致命错误,因为这意味着其他 Lua 调用也不太可能起作用
        std::cerr<<"处理脚本文件时发生致命的内存分配错误:" << luaScriptName << std::endl;
        return false;
}
return true;

但是我收到了“无法找到 / 打开 Lua 脚本文件:”的错误。

我是否应该将我的 script.lua 指向 Visual Studio?该文件位于项目目录中。

原文链接 https://stackoverflow.com/questions/5688781

点赞
stackoverflow用户2368
stackoverflow用户2368

Your code will exectue where the binary is, or in the target directory in Debugging Mode. So check that your lua file is accessible to your binary when you execute it. If it's with your source files, sure, it's not accessible.

你的代码将执行在二进制文件所在的位置,或在调试模式下的目标目录里。所以,在执行代码时,请确保你的lua文件对二进制文件是可访问的。如果它和源文件在一起,那么它肯定是无法访问的。

2011-04-16 19:52:12