lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On 14.08.2021 16:33, Spar wrote:
luaopen_test must be marked as __declspec(dllexport).
Also add extern "C" if you compile with C++ compiler

No exactly. __declspec(dllexport) is Windows specific way to export syombols from dynamic libraries. Here we are on Linux, and symbol is exported already

See...

   00000000000011ac T _Z12luaopen_testP9lua_State

It's C++ mungled, thus is not found with dlopen inside of Lua.

And yes, it should be marked extern "C" to avoid C++ mangling, i.e.

extern "C" int luaopen_test(lua_State *L) {
    luaL_newlib(L, mylib);
    return 1;
}


Regards,
Timur


Lua version: 5.3.5
OS: Voidlinux


g++ -fPIC -I/usr/include/lua5.3 -shared -Wall -o test.so test.cpp


lua
require("test")
error loading module 'test' from file './test.so':
./test.so: undefined symbol: luaopen_test


nm -g test.so
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
00000000000011ac T _Z12luaopen_testP9lua_State
w __cxa_finalize@@GLIBC_2.2.5
w __gmon_start__
U luaL_checknumber
U luaL_checkversion_
U luaL_setfuncs
U lua_createtable
U lua_pushnumber


test.cpp:
#include <lua.hpp>

static int l_test(lua_State *L) {
double num1 = luaL_checknumber(L, 1);
double num2 = luaL_checknumber(L, 2);
lua_pushnumber(L, num1 + num2);
return 1;
}

static luaL_Reg mylib [] = {
{"test", l_test},
{NULL, NULL}
};

int luaopen_test(lua_State *L) {
luaL_newlib(L, mylib);
return 1;
}


lua.hpp:
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}


Thank you in advance.