lua-users home
lua-l archive

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


Hello,

I'm using Lua 5.1.4 with windows.
I'd like to call C++ functions from a dll.
It doesn't work with MS-Windows (work under Linux).
(gcc version is 4.4.0)

When i do the "require" in lua, i got this:

> require "libfoo"
error loading module 'libfoo' from file '.\libfoo.dll':
       Le module spécifié est introuvable.


If i compile this code with gcc, it works.
So i think it's a name mangling problem.
I've looked the name with "nm", it seems good for me.

Here is the small code i use for testing:

// work with:
// gcc -shared testlua.c -o libfoo.dll -I/mingw/include/lua -llua51

// doesn't work with:
// g++ -shared testlua.c -o libfoo.dll -I/mingw/include/lua -llua51

#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#ifdef __cplusplus
}
#endif

static int lua_print(lua_State *L)
{
  printf ("called: lua_print\n");
  return 0;
}

  static const struct luaL_Reg foo_fx [] = {
    { "print", lua_print },
    { NULL, NULL } //dernier element obligatoire
  };

#ifdef __cplusplus
extern "C" {
#endif
  __declspec(dllexport) int luaopen_libfoo(lua_State *L)

// for Linux, symbol export is not the same (and you compile with -fvisibility=hidden
//  __attribute__((visibility("default"))) int luaopen_libfoo(lua_State *L)
  {
    luaL_register (L, "foo", foo_fx);
    return 1;
  }
#ifdef __cplusplus
}
#endif


Thank you.