[toc]

# 在 Lua 项目下添加调试用入口

main.cpp 放到 lua-5.4.6 目录下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>

extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}

int main(int argc, char** argv)
{
std::cout << "Hello World!\n";
int status, result;
lua_State* L = luaL_newstate(); /* create state */
if (L == NULL) {
//l_message(argv[0], "cannot create state: not enough memory");
return EXIT_FAILURE;
}
std::string cmd = "a = 7+11";
int r = luaL_dostring(L, cmd.c_str());

if (r == LUA_OK)
{
lua_getglobal(L, "a");
if (lua_isnumber(L, -1))
{
float a_in_cpp = (float)lua_tonumber(L, -1);
std::cout << "a_in_cpp = " << a_in_cpp << std::endl;
}
}
else {
std::string errormsg = lua_tostring(L, -1);
std::cout << errormsg << std::endl;
}

//luaL_dofile(L, "xxx.lua")

lua_close(L);
}

# 添加构建 CMakeLists.txt 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
cmake_minimum_required(VERSION 3.20)
project (LuaProject01) # project here actually means solution in premake

set(LUA_VERSION 5.4.3)

if(WIN32)
add_definitions("-DLUA_BUILD_AS_DLL")
add_definitions( -D_CRT_SECURE_NO_WARNINGS )
endif(WIN32)

aux_source_directory(src LUA_SOURCES)
list(REMOVE_ITEM LUA_SOURCES "src/lua.c" "src/luac.c")

set(LUA_LIBRARY lua${LUA_VERSION})
add_library(${LUA_LIBRARY} SHARED ${LUA_SOURCES})

add_executable(lua src/lua.c)
target_link_libraries(lua ${LUA_LIBRARY})
if(UNIX AND NOT APPLE) #add math library for linux
target_link_libraries(lua m)
endif()

add_executable(luac ${LUA_SOURCES} src/luac.c)
if(UNIX AND NOT APPLE)
target_link_libraries(luac m)
endif()

include_directories(src)
add_executable (LuaProject01 main.cpp)
target_link_libraries (LuaProject01 ${LUA_LIBRARY})
if(UNIX AND NOT APPLE) #add math library for linux
target_link_libraries( LuaProject01 m )
endif()

其中 LuaProject01 就是调试入口,之后 cmake . 即可

# 调试 Lua 源码文件

编写 main 函数如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main(int argc, char** argv)
{
int status, result;
lua_State* L = luaL_newstate(); /* create state */
if (L == NULL) {
//l_message(argv[0], "cannot create state: not enough memory");
return EXIT_FAILURE;
}
std::string cmd = "a = 7+11";
int r = luaL_dostring(L, cmd.c_str());

lua_getglobal(L, "a");
if (lua_isnumber(L, -1))
{
float a_in_cpp = (float)lua_tonumber(L, -1);
std::cout << "a_in_cpp = " << a_in_cpp << std::endl;
}

luaL_dofile(L, "D:/Software/lua-5.4.6/lua-5.1.5/Lua/test.lua");

lua_close(L);
}

其中 test.lua 文件如下:

1
print("Hello world")

但是点击执行后不会报错,但也不会有什么输出,原因是因为没有注册 lua 的 libs 修改后的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main(int argc, char** argv)
{
int status, result;
lua_State* L = luaL_newstate(); /* create state */
if (L == NULL) {
//l_message(argv[0], "cannot create state: not enough memory");
return EXIT_FAILURE;
}
luaL_openlibs(L);
std::string cmd = "a = 7+11";
int r = luaL_dostring(L, cmd.c_str());

lua_getglobal(L, "a");
if (lua_isnumber(L, -1))
{
float a_in_cpp = (float)lua_tonumber(L, -1);
std::cout << "a_in_cpp = " << a_in_cpp << std::endl;
}

luaL_dofile(L, "D:/Software/lua-5.4.6/lua-5.1.5/Lua/test.lua");

lua_close(L);
}

其中 LuaL_openlibs 的實現如下: