Why does the error "Error loading DLL" occur when I run the following program?
main.lua, sample.dll, and lua54.dll are in the same directory.
Maybe there's an error in the dll file?
----------
//sample.cpp
#include <iostream>
#include <lua.hpp>
void printTable(lua_State* L, int index) {
lua_pushnil(L);
while (lua_next(L, index) != 0) {
const char* key = lua_tostring(L, 1);
const char* value = lua_tostring(L, 2);
std::cout << "[cpp input] " << key << " : " << value << std::endl;
lua_pop(L, 1);
}
}
int cppFunction(lua_State* L) {
if (lua_gettop(L) != 1 || !lua_istable(L, 1)) {
return luaL_error(L, "Expected a table.");
}
printTable(L, 1);
lua_pushstring(L, "new key1");
lua_pushstring(L, "new value1");
lua_settable(L, 1);
lua_pushstring(L, "new key2");
lua_pushstring(L, "new value2");
lua_settable(L, 1);
printTable(L, 1);
lua_pushvalue(L, 1);
return 1;
}
extern "C" int luaopen_mystack(lua_State* L) {
lua_register(L, "cppFunction", cppFunction);
return 0;
}
----------
--main.lua
io.stdout:setvbuf("no")
function love.load()
sample = package.loadlib("sample.dll", "luaopen_mystack")
if sample then
sample() -- DLLの初期化関数を呼び出す
else
print("Error loading DLL")
end
end
function love.draw()
local myTable = {key1 = "value1", key2 = "value2"}
if cppFunction then
cppFunction(myTable)
for key, value in pairs(myTable) do
print(key, value)
end
else
print("cppFunction is not loaded")
end
love.graphics.print("Hello, world!", 400, 300)
end
----------
#CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(MyLuaProject)
# インクルードディレクトリを設定
include_directories(${CMAKE_SOURCE_DIR}/include)
# ソースファイルを指定
set(SOURCE_FILES src/main.cpp)
#set(SOURCE_FILES src/main.cpp src/sample.cpp)
# 共有ライブラリ(DLL)を作成
add_library(sample SHARED ${SOURCE_FILES})
# 静的ライブラリを指定
target_link_libraries(sample ${CMAKE_SOURCE_DIR}/lib/liblua54.a)
# ライブラリ名を設定
set_target_properties(sample PROPERTIES OUTPUT_NAME "sample")
# コマンドを実行する!
# cd ./build
# cmake -G "MinGW Makefiles" ..
# mingw32-make
----------
--console
Error loading DLL
cppFunction is not loaded
cppFunction is not loaded
cppFunction is not loaded