Page 1 of 1

requiring c module crashes love

Posted: Sat Jan 18, 2020 11:51 pm
by jaschutte
Hello everyone, hope you are having a lovely day.

Recently I've been trying to get to expose c functions to lua. Which had some problems and I'm still testing around but I managed to make it work. Atleast it works in regular 5.1 lua, but everytime I try to require the file into main.lua and execute main.lua with love it crashes. No error at all it just closes itself.
main.lua

Code: Select all

local mylib
print(pcall(function() mylib = require("mylib") end)) --prints nothing

function love.draw()
    love.graphics.rectangle("fill",0,0,200,200)
end
mod.c

Code: Select all

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

static int c_swap (lua_State *L) {
    double arg1 = luaL_checknumber (L, 1);
    double arg2 = luaL_checknumber (L, 2);

    lua_pushnumber(L, arg2);
    lua_pushnumber(L, arg1);

    return 2;
}

static const struct luaL_Reg func[] = {
      {"c_swap", c_swap},
      {NULL, NULL}
    };

__declspec(dllexport) int luaopen_mylib (lua_State *L){
    luaL_register(L, "mylib", func);
    return 1;
}

I compile mod.c into mylib.dll using the following command:

Code: Select all

gcc mod.c -shared -o mylib.dll -fPIC  -llua
.
When I require the dll with regular lua it works perfectly fine. I can all mylib.c_swap(1,2) and it'll work but in love it shutsdown the program instantly. I've tried searching for awnsers on the internet but I couldn't find anything. I'm completely lost on this one.

lua 5.1 and löve are 32 bit. Mingw is also 32 bit.
All of the files are in the same directory.

Re: requiring c module crashes love

Posted: Sun Jan 19, 2020 2:03 pm
by pgimeno
Maybe try with FFI? It may also have the additional benefit of not slowing down your program.

Re: requiring c module crashes love

Posted: Sun Jan 19, 2020 3:15 pm
by raidho36
luaL_register function might be removed from LuaJIT as it's 5.1 baseline with 5.2 features.
http://lua-users.org/wiki/CompatibilityWithLuaFive

Re: requiring c module crashes love

Posted: Thu Jan 23, 2020 9:38 pm
by monolifed
You are linking against lua and love uses luajit
Edit: It seems to be a little different for windows: http://lua-users.org/wiki/BuildingModules

Re: requiring c module crashes love

Posted: Fri Jan 24, 2020 7:10 am
by dwilso
Well, this is crazy. I recently wrote a neural network library in C and ported it to standard Lua, expecting to be able to simply require it with Love2D. I was having the exact same problem as OP, and dug through the internet without success. I came to create a forum post of my own and saw this thread.

ingsoc451's solution works perfectly (except you need the -lluajit gcc flag). I'm an OSX user using a .so library (as opposed to OP using a Windows .dll). I compiled with:

Code: Select all

gcc -shared -fpic -I/usr/local/include/luajit-2.0 -lluajit -o nnlib.so cailib/*.c
then simply required the library with:

Code: Select all

local NeuralNetwork = require "nnlib" 
and it's now working as expected.