0

Starting from this example i build C mylib.so library in order to call C API function.

I use lua5.4.4 building it on my own 'make mingw under msys2' and install it under msys2 /mingw32/bin, /include, etx.

I started from this sample code https://chsasank.com/lua-c-wrapping.html

Building it under msys2 with gcc -Wall-shared -fPIC -o mylib.so -I/mingw32/include mylib.c -llua

Then running ./libexample.lua with

#!/mingw32/bin/lua

package.cdpath = 'mylib'
local mylibtest = require 'mylib' 

Getting output as this error loading module 'mylib' from 'mylib'

  • `cdpath` or `cpath`? – Egor Skriptunoff May 27 '22 at 14:55
  • 1
    Where your .so file is located? Why do you need to modify `package.cpath`? – Egor Skriptunoff May 27 '22 at 14:56
  • mylib.so is at root directory of lua test file. – N i c o l a s May 28 '22 at 14:48
  • Try to remove the line `package.cdpath = 'mylib'` – Egor Skriptunoff May 28 '22 at 14:52
  • Already done. Same issue. I try too require './mylib.so'. Same issue. Is there possible incompatibility to try this sample under msys2 taking account that i make mingw lua 5.4.4 and getting lua.exe, install in /mingw32/bin ? – N i c o l a s May 28 '22 at 15:08
  • I tried this another sample highrestimer 'cause i need it, https://stackoverflow.com/questions/463101/lua-current-time-in-milliseconds. compiled under msys2 mingw32 with gcc -Wall -shared -fPIC -o timer.so -I/mingw32/include HighResTimer.c -llua – N i c o l a s May 28 '22 at 15:21
  • What does `/path/to/lua.exe -e"print(package.cpath)"` print on your system? – Egor Skriptunoff May 28 '22 at 15:24
  • It may happen `lua.exe` expects a module in `.dll`-file instead of `.so` – Egor Skriptunoff May 28 '22 at 15:25
  • This is the result /mingw32/bin/lua.exe -e"print(package.cpath)" C:\msys64\mingw32\bin\?.dll;C:\msys64\mingw32\bin\..\lib\lua\5.4\?.dll;C:\msys64\mingw32\bin\loadall.dll;.\?.dll And a run of Highrestimer.lua # ./HighResTimer.lua C:\msys64\mingw32\bin\lua.exe: error loading module 'timer' from file '.\timer.dll': La proc�dure sp�cifi�e est introuvable. stack traceback: [C]: in ? [C]: in function 'require' ./HighResTimer.lua:9: in main chunk [C]: in ? – N i c o l a s May 28 '22 at 17:17
  • Save your `mylib.dll` to `C:\msys64\mingw32\lib\lua\5.4\mylib.dll` – Egor Skriptunoff May 28 '22 at 17:46
  • Hum, that's what i obtain Mr Skriptunoff. Thank you for help. cp timer.dll /c/msys64/mingw32/lib/lua/5.4/ foo.bar@BAR-FOO MINGW32 /c/Users/foo.bar/source/Vim.8.2.5018/src/lua-5.4.4/src # ./HighResTimer.lua C:\msys64\mingw32\bin\lua.exe: error loading module 'timer' from file 'C:\msys64\mingw32\bin\..\lib\lua\5.4\timer.dll': La proc�dure sp�cifi�e est introuvable. stack traceback: [C]: in ? [C]: in function 'require' ./HighResTimer.lua:9: in main chunk [C]: in ? – N i c o l a s May 28 '22 at 20:24
  • You may have forgotten to change `luaopen_HighResolutionTimer` to `luaopen_timer`. The name of C function (luaopen_*) should correspond to the name of dll and the name of module (in "require"). – Egor Skriptunoff May 28 '22 at 22:06
  • As a tribute to this forum :) , getting C stack overflow. uploaded files https://drive.google.com/file/d/1vHDudS7UQj1XzzjNvv9kGP5D2k9oRceJ/view?usp=drivesdk The errors: # gcc -Wall -shared -fPIC -o HighResTimer.dll -I/mingw32/include HighResTimer.c -llua MINGW32/c/Users/foo.bar/source/Vim.8.2.5018/src/lua-5.4.4/src # ./HighResTimer.lua C:\msys64\mingw32\bin\lua.exe: error loading module 'HighResTimer' from file '.\HighResTimer.lua': C stack overflow stack traceback: [C]: in ? [C]: in function 'require' .\HighResTimer.lua:9: in main chunk – N i c o l a s May 29 '22 at 07:28
  • This is because of names collision: you have the main file `HighResTimer.lua` and library `HighResTimer.dll`. The names should be different. – Egor Skriptunoff May 29 '22 at 07:46
  • Require works. Thank you a lot – N i c o l a s May 29 '22 at 08:32
  • Uodate link if can help somebody https://drive.google.com/file/d/1vHDudS7UQj1XzzjNvv9kGP5D2k9oRceJ/view?usp=drivesdk. Thank a lot Mr Skriptunoff – N i c o l a s May 29 '22 at 09:00

0 Answers0