Lua设置搜索路径package.path和package.cpath
目录
Lua设置搜索路径package.path和package.cpath
--方法1 只加载想要的目录
package.path = "../myLuaTest/myLuaCode/?.lua;"
--方法2 增加目录
package.path = "../myLuaTest/myLuaCode/?.lua;"..package.path
print(package.path);
--搜索指定路径下,以.so结尾的文件
package.cpath = "../ybslib/bin/?.so;"..package.cpathpackage.cpath = "../ybslib/bin/?.so;"..package.cpath
注意VS里的Lua工程的Working Path的设置问题,如果将Lua工程设为启动项,那么.exe的搜索路径会变。
#include <lua.hpp>
#include <windows.h>
#include <iostream>
using namespace std;
int main() {
lua_State*l = luaL_newstate();
luaL_openlibs(l);
int ok = luaL_dofile(l, "../myLuaCode/LuaCode/main.lua");
if (ok == 1) {
cout << "1111111111";
ok = luaL_dofile(l, "main.lua");
}
lua_close(l);
system("pause");
}
比如启动C++项目的话,会走ok==0,启动Lua项目的话,ok搜多不到路径会返回1,这样会打印出111111那一行。