I have Lua 5.3.5 64-bit installed on my machine. I am compiling a 64-bit dll to test the c api process. Here is my file, driver.c:
#define LUA_LIB
#include "lua/lua.h"
#include "lua/lualib.h"
#include "lua/lauxlib.h"
static int returnone(lua_State *L) {
return 1;
}
static const luaL_Reg lualib[] = {
{"returnone", returnone},
{NULL, NULL}
};
int luaopen_lualib(lua_State *L) {
luaL_newlib(L, lualib);
return 1;
}
This outputs to lualib.dll
I created a script, test.lua in the same directory as lualib.dll.
require("lualib");
I get this:
$ lua.exe test.lua
C:\Program Files\Lua\lua.exe: error loading module 'lualib' from file '.\lualib.dll':
The specified procedure could not be found.
stack traceback:
[C]: in ?
[C]: in function 'require'
test.lua:1: in main chunk
[C]: in ?
Then I try
print(package.loadlib("lualib", "luaopen_lualib"));
And I get
$ lua.exe test.lua
nil The specified procedure could not be found.
init
I am stumped. Where's my library?
When building to Lua module to windows DLL you need to use __declspec(dllexport) e.g. this should be enough for the most simple cases:
__declspec(dllexport) int luaopen_lualib(lua_State *L) {
luaL_newlib(L, lualib);
return 1;
}
Please refer to Building Modules on lua-users.
As for a more verbose example I would suggest luasocket: source, header.
Related
I am building a DLL to embed Python 2.7 (2.7.18 to be specific) in a larger application largely following python.org's C API documentation. When I believe I am applying Py_DECREF correctly to a PyString PyObject, any linking application crashes without error. The code is largely copied from a similar application built with python 3.8, 3.9 and 3.10 that in fact works.
// python_functions.h
__declspec(dllexport) int init_python();
// python_functions.c
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <stdio.h> // printf
#include <string.h>
//#include <wchar.h> // wchar_t for python3 only
#include "python_functions.h"
__declspec(dllexport) int init_python() {
int rv = -1;
printf("attempting to initialize Python interpreter...\n");
// I have other python versions, Python27 not on path. set PYTHONHOME
Py_SetPythonHome("C:\\Python27\\"); // python27
//Py_SetPythonHome(L"C:\\Python38\\"); // python3X
Py_Initialize();
PyObject * pModule_Name = PyString_FromString("python_functions"); // python27 only
//PyObject * pModule_Name = PyUnicode_FromString("python_functions"); // python3X
if (!pModule_Name) {
printf("failed to convert module name to python string object\n");
return rv;
}
// can confirm pModule_Name != NULL && Py_REFCNT(pModule_Name) == 1 here
/*
load python module named "python_functions"
*/
// can also confirm pModule_Name != NULL && Py_REFCNT(pModule_Name) == 1 here
//Py_DECREF(pModule_Name); // If I uncomment this line, application linking .dll crashes
printf("pModule_Name successfully used\n");
rv = 0;
return rv;
}
// main.c
#include <stdio.h>
#include "python_functions.h"
int main(int argc, char **argv) {
int rv = 0;
// crashes without error if Py_DECREF(pModule_Name) in python_functions.c uncommented
printf("%d\n", init_python());
return rv;
}
Compiling the .dll with gcc 8.1.0 (MinGW-64) linking against C:\Python27\python27.dll:
gcc -IC:\Python27\include -LC:\Python27\libs -IC:\Python27 python_functions.c -shared -o python_functions.dll -lpython27
Compiling main with python27.dll and python_functions.dll in the same directory:
gcc main.c -o main.exe -L./ -lpython_functions
If I uncomment the line Py_DECREF(pModule_Name), which I thought I must do since PyString_FromString returns a new reference, then running main.exe crashes without return.
Should I be using something else or not doing reference counting on result from PyString_FromString or is this a larger problem with linking to python27?
If I make the commented/corresponding compiler changes to link against python 3.8+, no problems...
The only differences I know of are the significant changes to the Python C API between 2.7 and e.g. 3.8+ as well as the differences in python 2.X vs 3.X string representations. I believe PyString is appropriate here because when I actually implement and load a module name python_functions.py, it works...so long as I leave Py_DECREF(pModule_Name) commented out and the pointer leaked.
I also thought PyString_FromString might have a problem passing a string literal, but it accepts a const char *, documentation says it creates a copy, and even passing a copy of the string literal to another variable does not work.
I have looked at every post pertaining the "multiple Lua VMs detected" error and none of their answers worked. I have done everything the lua.org building guide says to compile it and it still shows the error. Using visual studio 2019 causes an unresolved external symbol error and using the GCC command without -llua also causes unresolved symbols. Any ideas. I am using version lua 5.3.2 from the binary compiled with the provided make file.
this is the code I am trying to use
#include "lua.h"
#include "lauxlib.h"
#include <Windows.h>
int test_function(lua_State* L)
{
return 0;
}
static const luaL_Reg testlib[] = {
{"test_function", test_function},
{NULL, NULL}
};
__declspec(dllexport) int __cdecl luaopen_testlib(lua_State* L)
{
luaL_newlib(L, testlib);
return 1;
};
local lib, err = package.loadlib([[module.dll]], "luaopen_testlib")
if lib ~= nil then
--multiple Lua VMs detected
lib()
else
print(err)
end
I would like to build a .dll filled with Lua bindings written in C compiled using VS 2017, but I don't seem to be having any luck, and the resources available to me are confusing and, by majority, outdated.
Here is what I've done.
I've already compiled lua from source and added it to my path so that I can lua.exe anything. This also created lua53.dll.
I've taken all of the .c and .h files of the lua source and added them to my VS project, along with one main.c which I am using to test. I added the .dll file as well, but only in the same way that I added the .c and .h files. I don't think it's doing anything.
Here's main.c:
#define LUA_LIB
#include "lua/lua.h"
#include "lua/lualib.h"
#include "lua/lauxlib.h"
#include "tg/tg.h"
static int lua_TG(lua_State *L) {
return 1;
}
static int lua_useTGHandle(lua_State *L) {
struct TGHandle *tgHandle = malloc(sizeof(struct TGHandle));
*tgHandle = TG();
lua_pushlightuserdata(L, tgHandle);
return 1;
}
static const luaL_Reg tglib[] = {
{"TG", lua_TG},
{"useTGHandle", lua_useTGHandle},
{NULL, NULL}
};
LUALIB_API int luaopen_libtg(lua_State* L) {
luaL_newlib(L, tglib);
return 1;
}
One function implemented, one not, but the lib should register.
I have changed the build type to a .dll, and I get the .dll generated without any errors, but when I try to use it in a Lua script I get:
%1 is not a valid Win32 application.
Surely I just have no clue what I'm doing. How would I just set up a lua-binding-building environment in VS2017?
SO basically i got the lua5.3.3 source code and i am trying to build it with mingw
what i have done so far is that i did the whole operation in msys and then copied over the lib files and bin files and include files to mingw appropriate folders
however when i try to actually compile an application that uses it, i get these errors
this is the command i used to compile my program that uses lua
gcc syx.cpp -llua
C:\Users\User\AppData\Local\Temp\cckJPF8N.o:syx.cpp:(.text+0xf): undefined reference to `luaL_newstate()'
C:\Users\User\AppData\Local\Temp\cckJPF8N.o:syx.cpp:(.text+0x21): undefined reference to `luaL_openlibs(lua_State*)'
C:\Users\User\AppData\Local\Temp\cckJPF8N.o:syx.cpp:(.text+0x3e): undefined reference to `luaL_loadfilex(lua_State*, char const*, char const*)'
C:\Users\User\AppData\Local\Temp\cckJPF8N.o:syx.cpp:(.text+0x77): undefined reference to `lua_pcallk(lua_State*, int, int, int, int, int (*)(lua_State*, int, int))'
C:\Users\User\AppData\Local\Temp\cckJPF8N.o:syx.cpp:(.text+0x87): undefined reference to `lua_close(lua_State*)'
collect2.exe: error: ld returned 1 exit status
and here is the file(very basic) in case you need to see it
#include <stdio.h>
#include <lua5.3/lua.h>
#include <lua5.3/lualib.h>
#include <lua5.3/lauxlib.h>
/* the Lua interpreter */
lua_State* L;
int main ( int argc, char *argv[] )
{
L = luaL_newstate();
luaL_openlibs(L);
luaL_dofile(L, "test.lua");
/* cleanup Lua */
lua_close(L);
return 0;
}
i know the library file exists since it was created with mingw namely liblua.a which sits in my mingw lib folder , as well as the other files related to lua such as lua.exe luac.exe the include files etc so i am not sure what else is missing
Okay i found the solution
turns out gcc is mangling the symbols from the library header(whatever that means)
what needs to be done is the headers need to be wrapped in a extern "C" {//headers} to make it work
reference is here :
http://www.linuxquestions.org/questions/programming-9/undefined-reference-error-when-using-lua-api-892782/
and just to be clear, here is a working example:
#include <stdio.h>
extern "C"{
#include <lua5.3/lua.h>
#include <lua5.3/lualib.h>
#include <lua5.3/lauxlib.h>
}
/* the Lua interpreter */
lua_State* L;
int main ( int argc, char *argv[] )
{
L = luaL_newstate();
luaL_openlibs(L);
luaL_dofile(L, "test.lua");
/* cleanup Lua */
lua_close(L);
return 0;
}
I have a lib, called "test.so" that uses functions from two libraries that reference each other. If I call the functions of test.so within a C program, it works just fine, so I assume there's no error in C functions.
However, when I call it from Lua, it throws a Seg Fault error due to an "undefined symbol". The problem is, the symbol is defined, I can see it when I run nm test.so.
Reading the thread below
Lua: C++ modules can't reference eachother, undefined symbol
I tried to creating a new module that would load test.so using dlopen, as described by Rena.
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <stdio.h>
#include <dlfcn.h>
int luaopen_drmaa(lua_State *L){
printf("Chamou luaopen_test\n");
if( dlopen("/home/erica/Downloads/test.so", RTLD_NOW | RTLD_GLOBAL) == NULL)
printf("%s\n", dlerror());
else
printf("Chamou dlopen e teve retorno nao null\n");
return 0;
}
I compiled it:
gcc -g drmaa_lib.c -shared -fpic -I /usr/include/lua5.1 -I /usr/local/include/ -L /usr/local/lib/m -llua5.1 -o drmaa.so
But when I run, I get:
$ lua5.1
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
> require "drmaa"
Chamou luaopen_test
Chamou dlopen e teve retorno nao null
> submitJob()
stdin:1: attempt to call global 'submitJob' (a nil value)
stack traceback:
stdin:1: in main chunk
[C]: ?
Then I tried inserting in main function
luaopen_test(L);
and above the main function
extern int luaopen_test(lua_State *L);
to actually open the lib, but I get this error:
$ lua5.1
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
> require "drmaa"
error loading module 'drmaa' from file './drmaa.so':
./drmaa.so: undefined symbol: luaopen_test
stack traceback:
[C]: ?
[C]: in function 'require'
stdin:1: in main chunk
[C]: ?
The author of the referenced question doesn't show details of the development of the solution.
Does anyone has a clue of what may make it work?
Thanks in advance.
I was obviously doing it wrong, to get the function that actually opens the library I should have used ldsym function. The new code for drmaa lib (the one that loads test.so) is
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <stdio.h>
#include <dlfcn.h>
typedef void Register(lua_State*);
int luaopen_drmaa(lua_State *L){
void* lib = dlopen("/home/erica/Downloads/test.so", RTLD_NOW | RTLD_GLOBAL);
if(!lib)
{
printf("%s\n", dlerror());
return 1;
}
Register* loadFunc = (Register*)dlsym(lib, "luaopen_test");
if(!loadFunc)
{
printf("%s\n", dlerror());
return 1;
}
loadFunc(L);
return 0;
}
This answer was base in some code of this thread:
Lua shared object loading with C++ segfaults