????????
//
????????L?
=
?luaL_newstate();
????????
//
?link?lua?lib
????????luaL_openlibs(L);
????????
int
?err?
=
?
0
;
????????err?
=
?luaL_loadfile(?L,?
"
./lua/test.lua
"
?);
????????
if
(?err?
!=
?LUA_OK?)
????????????printf(
"
Failed?to?load test.lua![%s]
"
,?lua_tostring(L,
-
1
));
一開始,沒有注意看錯(cuò)誤信息,一看后
Failed?to?call?test.lua![./lua/test.lua:7:?module?'test_fun'?not?found:
????????no?field?package.preload['test_fun']
????????no?file?'e:\testproj\lua\test.lua'
????????no?file?'e:\testproj\lua\test\init.lua'
????????no?file?'e:\testproj\test_fun.lua'
????????no?file?'e:\testproj\test_fun\init.lua'
????????no?file?'.\test_fun.lua'
????????no?file?'e:\testproj\test_fun.dll'
????????no?file?'e:\testproj\loadall.dll'
????????no?file?'.\test_fun.dll']]]
看清楚后,發(fā)現(xiàn)沒有,我lua文件的目錄
上網(wǎng)一找, 原來(lái)可以設(shè)置 package.cpath 的目錄,
原文在此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!
}
運(yùn)行后,又出現(xiàn) multiple Lua VMs detected 的錯(cuò)誤.
看回我的程序是 exe(link lua lib) --call-> test.lua --call--> ?.dll(link lua lib)
看完
這篇才記起來(lái),lua不能link多份(不然會(huì)出問(wèn)題,記得云風(fēng)blog有提過(guò))
于是乎,又把 lua 編譯成 dll
結(jié)果發(fā)現(xiàn) lua.dll 沒有導(dǎo)出函數(shù)
瞎蒙一陣后,發(fā)現(xiàn) LUA_API 的定義如下
#if?defined(LUA_BUILD_AS_DLL)????/*?{?*/
#if?defined(LUA_CORE)?||?defined(LUA_LIB)????/*?{?*/
#define?LUA_API?__declspec(dllexport)
#else????????????????????????/*?}{?*/
#define?LUA_API?__declspec(dllimport)
#endif????????????????????????/*?}?*/
#else????????????????/*?}{?*/
#define?LUA_API????????extern
#endif????????????????/*?}?*/
然后在工程又加了 LUA_BUILD_AS_DLL 的定義
最后,終于調(diào)用成功了,淚奔~~~ 三個(gè)小時(shí)不見了...