如何通过 C++ 全局加载 file.lua 库,而不必使用 loadfile("file.lua")() 导入它?

How can I load the file.lua library globally by C++ without having to import it with : loadfile("file.lua")()?

我在 github 上找到了一个名为“json.lua”的库:github json.lua

我正在通过 main.lua 文件导入这个库,如下所示:

local json = loadfile("json.lua")() -- json = library loaded
print("json decoded : "..json.decode("13E+2")) -- will print : json decoded = 1300.0 

但我想在全局范围内使用“json”变量,而无需使用 loadfile("json.lua")()

有没有办法加载json.luajson.lua文件字符串 直接全局进入 lua VM,以便任何其他文件( main1.lua、main2.lua、main3.lua、...lua )我只需键入“json.ANY_FUNCTION”去上班?

运行 在加载脚本之前在 C++ 中执行此操作:

luaL_dostring(L,"json = dofile('json.lua')");