lua-users home
lua-l archive

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


luaopen_test must be marked as __declspec(dllexport).
Also add extern "C" if you compile with C++ compiler
On 14 Aug 2021, 16:31 +0300, Tristan Kohl <tristankohl@web.de>, wrote:
Hello everybody,

I am trying to create bindings for a C++ library so I can use it in my
Lua project. However I seem to miss something. Maybe one of you could
help me. The module builds without errors but when I try to require the
.so file from Lua I get an error.

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.