stetre/moonlibs

Creating an executable that others can run

a327ex opened this issue · 5 comments

I spent quite a bit of time trying to figure out how to make scripts made with these libraries run on other people's computers so I figured this is something that could be on the README to make it easier for others. The way I did it was installing all libraries I needed using the Windows instructions, then creating a simple file that will read the script I want to run. That file looks like this:

#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

int main() {
  lua_State *L = luaL_newstate();
  luaL_openlibs(L);    

  if (luaL_loadfile(L, "main.lua") != LUA_OK) {
    printf("lua: %s\n", lua_tostring(L, -1));
  }

  if (lua_pcall(L, 0, 0, 0) != LUA_OK) {
    printf("lua: %s\n", lua_tostring(L, -1));
  }

  lua_close(L);
  return 0;
}

main.lua simply contains the code you want to run, in this case it contains the example script from the moongl readme. After compiling this with cc main.c -o main -llua you'll get an .exe and then you need bundle it with: the dlls from each library used in the script, in this case it would be moongl.dll and moonglfw.dll, which can be found in C:\msys64\mingw64\lib\lua\5.3; the Lua files that each library requires, which can be found in C:\msys64\mingw64\share\lua\5.3; and the dlls that each library uses. This was the hardest part because when you don't have those there's no error messages telling you exactly what's missing, it just says the module couldn't be loaded... For this example the needed dlls were glew32.dll and glfw3.dll which can be found in C:\msys64\mingw64\bin.

With all this together other people will be able to run exes created with these libraries. I only tested this for moongl and moonglfw so far but I imagine all other libraries are similar. If there's a simpler/more straightforward way of doing it I would also appreciate knowing about it if you could tell me. Thanks!

Hi, there is an easiest way, which I actually use in many of the examples bundled with the libraries: just write a main script in lua using the shebang directive #!/usr/bin/env lua in the first line, and make the file executable. You can then run the script as an ordinary executable.

This works on POSIX systems and thus also on MSYS. I don't have a Windows machine at hand to test it, but I presume that such a script should be executable also from the Windows command prompt (and thus from any GUI wrapper).

EDIT: Fixed a typo in the directive.

Oh, by "other people" I mean like general users who don't have anything like Lua or any of the libraries installed on their systems. I don't think doing that alone would work since the libraries aren't linked anywhere. I'm looking at a bunch of libraries to make games with and I found these which looked pretty good, but being able to have players run the executables is pretty important so that's why I posted this (in case it helps someone in the future!).

In any case, thanks a lot for your work on these libraries, they seem to be of really high quality. Probably the best I've seen for Lua anywhere. ^^

Thank you for the appreciation :)

The risk of potentially missing libraries is exactly the same either if you write the main as a script or as an executable. The C program you wrote is equivalent to running a script. If what you are aiming for is to give the users more meaningful hints when a library fails to load, you can do it in a script as well by just wrapping the require( ) calls in pcall().

(The real solution to distribute software to non-developer users is however a proper packaging of the final product for the platforms it targets, using their standard methods.)

Ah, I see what you mean now. Thanks for the time and sorry to bother you!

No bother at all!