requiring c module crashes love

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
jaschutte
Prole
Posts: 3
Joined: Sat Jun 29, 2019 12:43 pm

requiring c module crashes love

Post 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.
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: requiring c module crashes love

Post by pgimeno »

Maybe try with FFI? It may also have the additional benefit of not slowing down your program.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: requiring c module crashes love

Post 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
monolifed
Party member
Posts: 188
Joined: Sat Feb 06, 2016 9:42 pm

Re: requiring c module crashes love

Post 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
Last edited by monolifed on Tue Mar 10, 2020 6:07 am, edited 1 time in total.
dwilso
Prole
Posts: 1
Joined: Fri Jan 24, 2020 6:53 am

Re: requiring c module crashes love

Post 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.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 27 guests