Reputation: 99
I'm trying to make a c extension dll for lua. I have this csin.c
:
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <math.h>
static int l_sin(lua_State *L) {
double d = lua_tonumber(L, 1);
lua_pushnumber(L, sin(d));
return 1;
}
int luaopen_csin(lua_State *L) {
lua_register(L, "csin", l_sin);
return 0;
}
I'm compiling it with gcc -Wall -shared -Ilua-5.3.3/include -Llua-5.3.3/lib csin.c -o csin.dll -llua
. The include dir just has the .h
files from the Lua 5.3.3 src download and the lib dir has liblua.a
which I compiled with make generic
. It compiles without error, but when I try to use it in a Lua script, it just gives a Segmentation Fault
.
Here is lcsin.lua
:
require("csin")
print(csin(45))
I just run it with lua lcsin.lua
, lua
being a link to lua32/lua53.exe
, which I think I got from Lua's binary downloads. I tried using the compiled lua-5.3.3/src/lua.exe
too, but it says:
lua-5.3.3/src/lua: lcsin.lua:1: module 'csin' not found:
no field package.preload['csin']
no file '/usr/local/share/lua/5.3/csin.lua'
no file '/usr/local/share/lua/5.3/csin/init.lua'
no file '/usr/local/lib/lua/5.3/csin.lua'
no file '/usr/local/lib/lua/5.3/csin/init.lua'
no file './csin.lua'
no file './csin/init.lua'
no file '/usr/local/lib/lua/5.3/csin.so'
no file '/usr/local/lib/lua/5.3/loadall.so'
no file './csin.so'
stack traceback:
[C]: in function 'require'
lcsin.lua:1: in main chunk
[C]: in ?
I think that error is because the compiled one isn't looking for .dll
files but .so
files. I'm using cygwin. Windows/Linux inconsistencies have been a pain in the *** for a while now. I've tried example code online, and nothing works.
Upvotes: 2
Views: 483
Reputation: 26744
If the issue is indeed with searching for .so
files instead of *.dll
files, it should be easy to fix by adding the appropriate pattern to package.cpath
:
package.cpath = package.cpath..";./?.dll"
require("csin")
Assuming csin.dll
is in the current folder, it should be able to find and load it.
Upvotes: 2