'Requiring C++ DLL from Lua gives an error

I am trying to compile C++ code to a DLL file to use with Lua. To do this, I am using Visual Studio 2022 with Windows 10. My goal is to use the Lua require(modname) function to require my DLL and use the C++ methods in Lua. For now the DLL and the main.lua files are in the same directory. What is actually happening is that I get an error message saying:

error loading module 'lgf' from file 'lgf.dll': The specified module could not be found.

with the Lua code:

package.cpath = "?.dll;"..package.cpath
create = require("lgf")

My main.cpp file contains the following:

#include <iostream>
#include <string>

#include <window/include/window.hpp>
#include <font/include/font.hpp>
#include <image/include/image.hpp>
#include <keyboard/include/keyboard.hpp>
#include <mouse/include/mouse.hpp>
#include <rectangle/include/rectangle.hpp>

#include <lua/lua.hpp>

#define LIB

SDL_Window* sdlWin;
SDL_Renderer* sdlRen;

window win(sdlWin, sdlRen);

static const luaL_Reg lib[] = {
    {"create", window::create},
    {"isCloseRequested", window::isCloseRequested},
    {"sync", window::sync},
    {"update", window::update},
    {"setVSync", window::setVSync},
    {"setIcon", window::setIcon},
    {"clearScreen", window::clearScreen},
    {"render", window::render},
    {"windowChangeColor", window::changeColorRGB},
    {"close", window::close},

    {"createRectangle", rectangle::create},
    {"changeRectangleColor", rectangle::changeColor},
    {"drawRectangle", rectangle::draw},

    {"mouseButtonUp", Mouse::mouseButtonUp},
    {"mouseButtonDown", Mouse::mouseButtonDown},

    {"keyup", Keyboard::keyup},
    {"keydown", Keyboard::keydown},

    {"loadImage", ImageLoader::loadImage},
    {"drawImage", ImageLoader::drawImage},

    {"loadFont", FontLoader::loadFont},
    {"loadText", FontLoader::loadText},
    {"renderText", FontLoader::renderText},

    {NULL, NULL}
};

extern "C" __declspec(dllexport) int luaopen_lgf(lua_State* L) {
    luaL_newlib(L, lib);
    return 22;
}

I am creating a simple graphics library. My dependencies are SDL2 and (of course) Lua.

I have tried creating an entry point (main) for the file, and I still get the same message. Let me know if I should include my other .cpp files in here.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source