4

I want to code some functions in C to use in Lua and the easiest way to do this I think I can find is using LuaJIT's FFI.

I have a C file "add.c":

int add(int a, int b){
return a+b;
}

I assemble it into "add.o" with:

gcc -c add.c

I make "add.dll":

gcc - shared -o add.dll add.o

Finally, I try to run the following Lua code in LuaJIT:

local ffi =require("ffi")

local test=ffi.load("C:\\users\\quebe\\Desktop\\add")

ffi.cdef[[
int add(int a,int b);
]]

print(test.add(1,2))

and get:

luajit: test.lua:3: cannot load module 'C:\users\quebe\Desktop\add': %1 is 
not a valid Win32 application.

stack traceback:
    [C]: in function 'load'
    test.lua:3: in main chunk
    [C]: at 0x7ff72be120c0

but I have no idea how to interpret this to debug.

CircArgs
  • 570
  • 6
  • 16
  • Maybe try "add.dll" instead of "add"? – IS4 Aug 13 '17 at 01:56
  • The docs say http://luajit.org/ext_ffi_api.html it'll look for the .dll, but I even tried .so (I am on windows though) too to no avail. – CircArgs Aug 13 '17 at 02:54
  • I'm relatively novice with C and especially dynamic libraries do I'm thinking I missed something in the assembly or something in the C script. – CircArgs Aug 13 '17 at 02:57
  • What platform are you using? Windows, Linux, something else? – Nicol Bolas Aug 13 '17 at 05:51
  • 2
    @CircArgs - Did you export function `add` from your library? – Egor Skriptunoff Aug 13 '17 at 06:15
  • @Nicol: Windows. – CircArgs Aug 13 '17 at 13:02
  • @Egor Skriptunoff - I don't think I did as everything I did is in my question. I think I saw something about that but I was under some kind of impression it was compiler specific maybe. Could you elaborate? – CircArgs Aug 13 '17 at 13:04
  • @Egor Skriptunoff - https://cygwin.com/cygwin-ug-net/dll.html I had followed this which had nothing on "exporting" so Im guessing this is something to do with LuaJIT? – CircArgs Aug 13 '17 at 13:08
  • I'd suggest testing if your dll is usable by another c program. Quick google tells that the process of dll creation is not completely braindead: https://stackoverflow.com/questions/6721364/creating-a-dll-in-gcc-or-cygwin – Dimitry Aug 13 '17 at 14:08
  • @Dimitry prefixing the function with __declspec(dllexport) solved the issue. I'm not sure if this is how it's done, but since your comment led to the solution, would you like to edit your previous answer and I can accept it – CircArgs Aug 14 '17 at 03:34

3 Answers3

3

According to this, there should be declaration of the C function before loading the dll:

local ffi =require("ffi")
ffi.cdef[[
   int add(int a, int b)
]]
local test=ffi.load("C:\\users\\quebe\\Desktop\\add")

addendum:

Additionally, as Egor Skriptunoff mentioned, functions inside the dll file should be declared as exported. The specifics are given in this SO answer.

Dimitry
  • 2,204
  • 1
  • 16
  • 24
  • Thanks for that. I think that's an issue, but it doesnt seem to be my issue. I put it in the code and the same error came so I think it must be a problem making the .dll? – CircArgs Aug 13 '17 at 13:01
  • I've only used luajit on linux, My expertise ends here. I'll put some suggestions on dll in comments to the question itself. – Dimitry Aug 13 '17 at 14:06
0

This sounds like the dll you are trying to load has dependency dll's that cant be loaded - usually because they arent in Windows search path.

Ideas for solving:

  • Grab dependency walker and look at your dll - missing ones are usually marked red.
  • Often if you build with Visual Studio you may need visual studio runtimes like vcp140.dll and vcr140.dll (or similar). These are sometimes not correctly registered, and you may need to do that.
  • On Windows you must make sure that when you build the dll, all functions are exported (this can be done in a couple of ways - see MSVC help) and that they have __stdcall or __cdecl name mangling. Without this, you wont be able to call the functions using ffi.
  • Finally, watch out for security disabling of dlls. Sometimes dlls will be marked as not usable by the Win10 system. Check the properties and enable the dll. This usually happens when copying a dll between certain types of folders.
Dharman
  • 30,962
  • 25
  • 85
  • 135
David Lannan
  • 169
  • 2
  • 5
0

I had the same error while trying to do something related, turns out the LuaJIT binary was compiled in 64 bits and the .dll I was trying to load was compiled in 32 bits.

999PingGG
  • 54
  • 1
  • 5