title: unable to load c dll module in Lua
body: <p>I try to test loading a c++ dll module in Lua using "require", below is the c++ module file</p>
  
  <pre><code> #include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  
  extern "C" {
      #include "lua/lualib.h"
      #include "lua/lauxlib.h"
      #include "lua/lua.h"
  
      __declspec(dllexport) int luaopen_mylib(lua_State* L);
  }
  
  using namespace std;
  
  static int libFunc1(lua_State* L)
  {
      int n = lua_gettop(L);
      printf("in myfunc stack, arg number: %d\n", n);
      if (lua_isstring(L, -1))
      {
          std::cout &lt;&lt; lua_tostring(L, -1) &lt;&lt; std::endl;
      }
      else
      {
          std::cout &lt;&lt; "invalid arg" &lt;&lt; std::endl;
      }
      return 1;
  }
  
  static const struct luaL_Reg mylib[] = {{"func1", libFunc1}, {NULL, NULL}};
  
  int luaopen_mylib(lua_State* L)
  {
      cout &lt;&lt; "loading my lib" &lt;&lt; endl;
      luaL_newlib(L, mylib);
      return 1;
  }
  </code></pre>
  
  <p>I compiled this cpp file into dll using g++ in msys:</p>
  
  <pre><code>g++    -c -o mylib.o mylib.cpp
  g++ -shared -o mylib.dll mylib.o -Llua -llua5.3.0
  </code></pre>
  
  <p>until now everything work fine, and I got the mylib.dll file too. but when I try to load the module, I got the error msg:</p>
  
  <pre><code>&gt; require("mylib")
  error loading module 'mylib' from file '.\mylib.dll':
          找不到指定的程序。
  
  stack traceback:
          [C]: in ?
          [C]: in function 'require'
          stdin:1: in main chunk
          [C]: in ?
  </code></pre>
  
  <p>the Chinese characters above mean:</p>
  
  <pre><code>The specified function could not be found.
  </code></pre>
  
  <p>I think the "specified function" mean the "luaopen_mylib", but the cpp file do have the function:luaopen_mylib, WHAT IS GOING WRONG?</p>