[Solved] Failed to import dll in 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.
shimatsukaze
Prole
Posts: 10
Joined: Fri Oct 07, 2016 7:51 am

[Solved] Failed to import dll in LOVE

Post by shimatsukaze »

(Sorry I'm not a native speaker of English)

I wrote a dll file in C++ and tried to import it in LOVE, using

Code: Select all

require("TestDll")
however, LOVE exited very quickly without any error message.
But if I simply run it in lua instead of LOVE,

Code: Select all

require("TestDll")
ss.sayHello()
It executed correctly.
I just want to calculate something in C++, as I'm too lazy to translate my codes into lua.
So is importing dlls forbidden in LOVE? Are there alternative methods to use C++ functions in LOVE?
Last edited by shimatsukaze on Sat Nov 12, 2016 2:45 am, edited 1 time in total.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Failed to import dll in LOVE

Post by s-ol »

shimatsukaze wrote:(Sorry I'm not a native speaker of English)

I wrote a dll file in C++ and tried to import it in LOVE, using

Code: Select all

require("TestDll")
however, LOVE exited very quickly without any error message.
But if I simply run it in lua instead of LOVE,

Code: Select all

require("TestDll")
ss.sayHello()
It executed correctly.
I just want to calculate something in C++, as I'm too lazy to translate my codes into lua.
So is importing dlls forbidden in LOVE? Are there alternative methods to use C++ functions in LOVE?
you probably built your dll against a different version of lua. Love uses luajit/lua5.1 (it's built with luajit, but it's binary-compatible to luajit5.1).

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
shimatsukaze
Prole
Posts: 10
Joined: Fri Oct 07, 2016 7:51 am

Re: Failed to import dll in LOVE

Post by shimatsukaze »

s-ol wrote:
shimatsukaze wrote:(Sorry I'm not a native speaker of English)

I wrote a dll file in C++ and tried to import it in LOVE, using

Code: Select all

require("TestDll")
however, LOVE exited very quickly without any error message.
But if I simply run it in lua instead of LOVE,

Code: Select all

require("TestDll")
ss.sayHello()
It executed correctly.
I just want to calculate something in C++, as I'm too lazy to translate my codes into lua.
So is importing dlls forbidden in LOVE? Are there alternative methods to use C++ functions in LOVE?
you probably built your dll against a different version of lua. Love uses luajit/lua5.1 (it's built with luajit, but it's binary-compatible to luajit5.1).
I use C:\Program Files (x86)\Lua\5.1\lib\lua5.1.lib, so the problem is that lua and luajit are different?
I simply installed lua 5.1 and use header files of it, like lua.h and lualib.h.
So I have to install luajit which would solve this problem?
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Failed to import dll in LOVE

Post by s-ol »

shimatsukaze wrote: I use C:\Program Files (x86)\Lua\5.1\lib\lua5.1.lib, so the problem is that lua and luagit are different?
I simply installed lua 5.1 and use header files of it, like lua.h and lualib.h.
So I have to install luagit which would solve this problem?
no, it should be working like that.

If it's not the Lua Version, it's probably an 32/64bit problem. Your lua.h and lua5.1.lib are probably 32 bit if they are in Program Files x86, and I suppose your love.exe is in "regular" Program Files and x64? you need to make sure they are either both x32 or both x64.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
shimatsukaze
Prole
Posts: 10
Joined: Fri Oct 07, 2016 7:51 am

Re: Failed to import dll in LOVE

Post by shimatsukaze »

s-ol wrote: no, it should be working like that.

If it's not the Lua Version, it's probably an 32/64bit problem. Your lua.h and lua5.1.lib are probably 32 bit if they are in Program Files x86, and I suppose your love.exe is in "regular" Program Files and x64? you need to make sure they are either both x32 or both x64.
My lua is 32-bit. And I checked LOVE in the task manager and found that my love.exe is also 32-bit. Actually, I only downloaded the 32-bit version of LOVE.
And if I rename the dll, LOVE reports that "Error: error loading module 'TestDll2' from file '.\TestDll2.dll':"
So it is consistent with the dll?

TestDll.cpp

Code: Select all

#include <cstdio>
#include "TestDll.h"
static int averageFunc(lua_State *L)
{
	int n = lua_gettop(L);
	double sum = 0;
	int i;
	for (i = 1; i <= n; i++)
		sum += lua_tonumber(L, i);
	lua_pushnumber(L, sum / n);
	lua_pushnumber(L, sum);
	return 2;
}
 
static int sayHelloFunc(lua_State* L)
{
	printf("hello world!");
	return 0;
}
	
static const struct luaL_Reg myLib[] = 
{
	{"average", averageFunc},
	{"sayHello", sayHelloFunc},
	{NULL, NULL}
};
 
int luaopen_TestDll(lua_State *L)
{
	luaL_register(L, "ss", myLib);
	return 1;
}
TestDll.h

Code: Select all

#pragma once
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#ifdef LUA_API
#undef LUA_API
#endif
#ifdef LUA_EXPORTS
#define LUA_API __declspec(dllexport)
#else
#define LUA_API __declspec(dllimport)
#endif

extern "C" LUA_API int luaopen_TestDll(lua_State *L);
(Some warnings appear while compiling this... I've never written dlls before)
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Failed to import dll in LOVE

Post by raidho36 »

Since LÖVE uses LuaJIT, which can load raw binary libraries, you don't need to build Lua-specific ones. All you need to do is to build a regular library, use ffi.load and load header through ffi.cdef. Then you can call the library functions via FFI module.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Failed to import dll in LOVE

Post by zorg »

I might be wrong, but I only read that ffi.load can load in C libraries, not C++.
SCiENcE's lovemidi requires in a C++ dll, and i know that works.

That said, one other issue might be this: Are you putting the dll file where love.exe and its own dll files are? Because that's the only place from where it can require it.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Failed to import dll in LOVE

Post by raidho36 »

He's already using a DLL so how any of that is an issue?
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Failed to import dll in LOVE

Post by zorg »

raidho36 wrote:He's already using a DLL so how any of that is an issue?
Well, if i'm right, then it's an issue in that your ffi.load suggestion would be wrong, but more importantly, he might have the dll inside his project folder, which may exist anywhere on his PC; which will not work when he requires the dll from that folder, since as i said, it only works if the dll is placed next to (aka in the same place as) the löve binaries, i.e. the exe, and its own dll files.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Failed to import dll in LOVE

Post by raidho36 »

You can't really use a C++ library that was built with a different compiler than the main executable, due to name mangling and all the other fun C++ stuff. You should instead use C wrapping, as it is was done for long time now. This wouldn't have been an issue had C++ standard these things properly defined, but that's not meant to be. Also this may be the culprit of this particular case, since LuaJIT can't load a C++ library that was built with a different compiler.
Post Reply

Who is online

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