为什么 Lua 不能加载我的 DLL 文件?

我收到了这个错误信息:

FATAL ERROR: lua_pcall() failed: error loading module 'foo' from file 'C:\Users\Commander\Desktop\Platform\Debug\foo.dll': The specified procedure could not be found.

我在 Lua 中加载 DLL 文件:

require("foo")

我的 DLL 代码,我使用 Visual Studio 编译器 (编译时没有错误)

#include "stdafx.h"

#pragma comment( lib, "liblua53.a" )
extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}

#include <iostream>

using namespace std;

extern "C" {
    static int l_bar(lua_State *L)
    {
        puts("l_bar called.");
        return 0;
    }

    void luaopen_foo(lua_State *L)
    {
        static const struct luaL_Reg foo[] = {
        { "bar", l_bar },
            { NULL, NULL }
        };
        luaL_newlib(L, foo);
        lua_setglobal(L, "foo");
    }
}

有没有办法避免这个错误?或者说 Lua 不能加载 DLL 文件吗?

我在这里找到了这个 C++ 代码示例:https://stackoverflow.com/a/12059441/8767704

点赞