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
Related
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.
UPDATED: Problem solved. The dll must not be statically linking to lua, otherwise it crashes with a multiple Lua VMs detected exception. The code blow actually works fine, just leave it here in case someone got this problem too.
And wireshark uses lua5.2 because there's a "lua52.dll" in it's folder.
I'm writing wireshark plugin, some algorithm in C is difficult to implement in Lua, so I try to use these algorithm through dll.
Most examples online use the old version of Lua, which use luaL_register in the dll code. The luaL_register is replaced by lua_newtable/luaL_setfuncs in newer version, but I didn't find any working example online.
Here's what I tried :
#include <stdio.h>
#include <string.h>
#include "lua.hpp"
#include <windows.h>
extern "C" {
static int add(lua_State* L)
{
MessageBox(0, "", "", 0);
double op1 = luaL_checknumber(L,1);
double op2 = luaL_checknumber(L,2);
lua_pushnumber(L,op1 + op2);
return 1;
}
static luaL_Reg mylibs[] = {
{"add", add},
{0, 0}
};
__declspec(dllexport)
int luaopen_mylib(lua_State* L)
{
lua_newtable(L);
luaL_setfuncs(L, mylibs, 0);
lua_setglobal(L, "mylib");
return 1;
}
}
and the lua code:
require "mylib" -- <----------crashes
-- local mylib = package.loadlib("mylib.dll","luaopen_mylib");
print (mylib)
if(mylib)then
--mylib();
else
-- Error
end
local b=mylib.add(11,33);
print("sum:", b);
The lua code crashes at first line. How to fix it?
Another question, how to verify which version of Lua is wireshark using? Calling print(_VERSION) in wireshark's lua console, it shows nothing.
The crash occurs when statically linking to lua.lib, I guess there is already a lua VM in lua.lib, so use dynamic linking and the problem is gone.
I am learning C Windows RPC programming. Here is the source code for a dummy RPC Server I wrote and compiled without errors:
#include <stdio.h>
#include "md5_h.h"
#include "rpc.h"
#include "rpcndr.h"
int main() {
RPC_STATUS status;
status = RpcServerUseProtseqEp(
(RPC_WSTR)("ncacn_ip_tcp"),
RPC_C_PROTSEQ_MAX_REQS_DEFAULT,
(RPC_WSTR)("9191"),
NULL);
if (status) { exit(status); }
status = RpcServerRegisterIf(
md5_v1_0_c_ifspec,
NULL,
NULL);
if (status) { exit(status); }
status = RpcServerListen(
1,
RPC_C_LISTEN_MAX_CALLS_DEFAULT,
FALSE);
if (status) { exit(status); }
return 0;
}
void __RPC_USER midl_user_free(void* p) {
free(p);
}
void md5(const unsigned char* szMsg) {
printf("voila %s\n", szMsg);
}
The midl files get compiled without errors as well. The MIDL-compilation produces md5_s.c and md5_c.c as expected. Here is md5.idl file if needed:
[
uuid(D86FBC01-D6A7-4941-9243-07A4EC65E8CB),
version(1.0),
]
interface md5
{
void md5([in, string] const char* szMsg);
};
During the Linkage stage the following errors are produced:
LNK2019: unresolved external symbol __imp__RpcServerListen referenced in function main
I have same errors for every RPC-specific functions, such as RpcServerRegisterIf or RpcServerUseProtseqEp. I am using Microsoft Visual Studio 2013.
I think this comes from a missing include; but I can't figure which one. I tried to include rpc.h, without any change.
Do I have to include in my project the produced md5_s.c? I have tried so without resolving anything.
Thanks for helping!
You need to link against Rpcrt4.lib.
If you are using visual studio, add it in the Project -> Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies.
I tried to write a simple D Program and use it to access a simple C library but there is unknown error.
My c Code, Box.c
#include "Box.h"
int SayHello( int _int)
{
_int ++;
return _int;
}
My c header file, Box.h
#ifndef BOX_H_INCLUDED
#define BOX_H_INCLUDED
/* export interfaces */
#ifdef __cplusplus
extern "C" {
#endif
int SayHello( int _int);
#ifdef __cplusplus
}
#endif
#endif // BOX_H_INCLUDED
I compile it
gcc -c Box.c Box.h
resulting files
Box.o
Box.h.gch
I place them to my D Program's project directory
My D Code
module main;
import std.stdio;
import std.conv;
import std.c.stdio;
import clib;
int main(string[] args)
{
// test external c library
auto s = to!string( SayHello(3) ) ;
writefln( "my int is "~ s );
readln();
return 0;
}
My D interface file ( clib ), trying to link to my C library
module clib;
import std.c.stdio;
extern (C) int SayHello( int _int);
The error I get when I compile it using codeblocks
Compiling: hello.d
Linking console executable: bin/Debug/tutorial03-access-c-library4
obj/Debug/hello.o: In function `_Dmain':
/home/hamilton/Tutorial/tutorial03-access-c-library4/hello.d:11: **undefined reference to `SayHello'**
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
0 errors, 0 warnings
Error is "undefined reference to `SayHello'"
There is no error I get when I compile it using command in console
$ dmd Box.o hello.d clib.di
it will be very painful if I cannot use codeblocks as I need the debugging functionality.
Thanks
Update:
Compiler setting in codeblocks as followed
Linker for dynamic libs: gcc -m32 -lrt
Linker for static libs: ar
Debugger: gdb
You can change the build options in CodeBlocks from Project -> Build Options, Compiler settings -> Other options. The simplest thing to do would be to just add Box.o to Other options.
I am trying to write a program that uses a C API. This API just provides a api.h header file, a .dll and a .lib.
It is using __stdcall exports.
I tried including the api.h and adding the .lib and .dll file, but I still have the following error to the libraries path, but I still have the following error:
**** Build of configuration Debug for project TestLibsp ****
**** Internal Builder is used for build ****
gcc -LC:\Users\nbarraille\workspace\TestLibsp\lib -oTestLibsp.exe src\main.o
src\main.o: In function `main':
C:\Users\nbarraille\workspace\TestLibsp\Debug/../src/main.c:83: undefined reference to `sp_session_create#8'
C:\Users\nbarraille\workspace\TestLibsp\Debug/../src/main.c:86: undefined reference to `sp_error_message#4'
C:\Users\nbarraille\workspace\TestLibsp\Debug/../src/main.c:92: undefined reference to `sp_session_login#12'
collect2: ld returned 1 exit status
Build error occurred, build is stopped
Time consumed: 721 ms
Here is my .c file:
#include "api.h"
int main(int argc, char **argv){
sp_session *sp;
sp_error err;
sp_session_config spconfig;
/* Create session */
sp_error err = sp_session_create(&spconfig, &sp);
}
And here is the definition of the function I am trying to call in the api.h
#ifndef SP_LIBEXPORT
#ifdef _WIN32
#define SP_LIBEXPORT(x) x __stdcall
#else
#define SP_LIBEXPORT(x) x
#endif
#endif
SP_LIBEXPORT(sp_error) sp_session_create(const sp_session_config *config, sp_session **sess);
Any idea?
Thanks!
You need to link against the library with gcc -l<libname> -L<libpath>. I guess you are using eclipse, so you can easily add the lib in Project -> Properties -> C/C++Build -> Settings -> GCC C++ Linker -> Library