Problem calling c++ from Lua (MS-Windows version)

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem calling c++ from Lua (MS-Windows version)

jseb
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.


Reply | Threaded
Open this post in threaded view
|

Re: Problem calling c++ from Lua (MS-Windows version)

jseb
I did a try with Visual C++ (express edition, the one you get for free
on microsoft website)

Here is my compilation on command line:

set incpath=%vcinstalldir%\include
set libpath=%vcinstalldir%\lib
cl foo.cpp -c /I%incpath%\lua
link -dll foo.obj lua51.lib

And i get "foo.dll", of course.
When i try require "foo" in lua, i still get :

 > require "foo"
error loading module 'foo' from file '.\foo.dll':
         La procÚdure spécifiée est introuvable.

(same as my previous try with g++)


Here is the source-file.
Are there any windows user who could help me, and explain how to compile
it, whatever toolchain you use (Visual C, mingw ...).
Thank you!

I rewrite the source file, now it's very simple. It still works without
problems in Linux.



extern "C" {
#include <stdio.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

static int lua_print(lua_State*);
__declspec(dllexport)
int luaopen_libfoo(lua_State*);
}

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
};

__declspec(dllexport)
int luaopen_libfoo(lua_State *L)
{
   luaL_register (L, "foo", foo_fx);
   return 1;
}


Reply | Threaded
Open this post in threaded view
|

回复: Re: Problem calling c++ from Lua (MS-Windows version)

zhiguo zhao

Rename luaopen_libido to luaopen_foo

Write with HTC g7.

在 2011-2-22 上午12:37,"jseb" <[hidden email]>编写:

I did a try with Visual C++ (express edition, the one you get for free on microsoft website)

Here is my compilation on command line:

set incpath=%vcinstalldir%\include
set libpath=%vcinstalldir%\lib
cl foo.cpp -c /I%incpath%\lua
link -dll foo.obj lua51.lib

And i get "foo.dll", of course.
When i try require "foo" in lua, i still get :

> require "foo"
error loading module 'foo' from file '.\foo.dll':
       La procÚdure spécifiée est introuvable.

(same as my previous try with g++)


Here is the source-file.
Are there any windows user who could help me, and explain how to compile it, whatever toolchain you use (Visual C, mingw ...).
Thank you!

I rewrite the source file, now it's very simple. It still works without problems in Linux.



extern "C" {


#include <stdio.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

static int lua_print(lua_State*);
__declspec(dllexport)
int luaopen_libfoo(lua_State*);


}

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

static con...

__declspec(dllexport)
int luaopen_libfoo(lua_State *L)

{
 luaL_register (L, "foo", foo_fx);
 return 1;
}

Reply | Threaded
Open this post in threaded view
|

Re: 回复: Re: Problem calling c++ from Lua (MS-Windows version)

jseb
> Rename luaopen_libido to luaopen_foo

Thanks for the suggestion

"libido" ... that's a nice lapsus :)

Anyway, it was not working either.


Finally, i've found it:

I was launching the lua interpreter on MSYS or in CMD (windows shell):
same error with both of them.

Then i tried to write a little script (just the "require" on my lib) and
then , launched it directly via "windows explorer". I've got an error of
a missing dll this time.
So... the answer after a few googling:

with mingw, compile it with flag "-static-libgcc"

No idea how to manage this with Visual Studio.

Now the "require" work , whatever environment: MSYS, CMD, and "windows
explorer".

Windows mysteries :/


Reply | Threaded
Open this post in threaded view
|

Re: 回复: Re: Problem calling c++ from Lua (MS-Windows version)

Jerome Vuarand
2011/2/24 jseb <[hidden email]>:

>> Rename luaopen_libido to luaopen_foo
>
> Thanks for the suggestion
>
> "libido" ... that's a nice lapsus :)
>
> Anyway, it was not working either.
>
>
> Finally, i've found it:
>
> I was launching the lua interpreter on MSYS or in CMD (windows shell): same
> error with both of them.
>
> Then i tried to write a little script (just the "require" on my lib) and
> then , launched it directly via "windows explorer". I've got an error of a
> missing dll this time.
> So... the answer after a few googling:
>
> with mingw, compile it with flag "-static-libgcc"
>
> No idea how to manage this with Visual Studio.
>
> Now the "require" work , whatever environment: MSYS, CMD, and "windows
> explorer".
>
> Windows mysteries :/

A very useful tool to debug such DLL problems is Dependency Walker
[1]. It can tell you what dependencies a DLL has (and it displays
warning if it's unable to find them). It is also useful to examine
exported symbols, and check that your luaopen_foo is properly named
and not mangled.

And to find out why -static-libgcc solves the problem, and how to
solve it with MSVC, you will have to provide more details, such as
what DLL exactly is reported missing, what unusual things you might
have in your PATH, or whether you may have several foo.dll files in
the PATH (for example one for the C/C++ library, and one for the Lua/C
binding module).

[1] http://www.dependencywalker.com/

Reply | Threaded
Open this post in threaded view
|

Re: 回复: Re: Problem calling c++ from Lua (MS-Windows version)

Michael Gogins
It is also useful to know that dependency walker can profile a running
program. In this case, that would be the Lua runtime, with command
line arguments specifying the script to run. I always enable full
paths in the trace messages so I can see where the program is actually
loading libraries from.

On Thu, Feb 24, 2011 at 12:38 PM, Jerome Vuarand
<[hidden email]> wrote:

> 2011/2/24 jseb <[hidden email]>:
>>> Rename luaopen_libido to luaopen_foo
>>
>> Thanks for the suggestion
>>
>> "libido" ... that's a nice lapsus :)
>>
>> Anyway, it was not working either.
>>
>>
>> Finally, i've found it:
>>
>> I was launching the lua interpreter on MSYS or in CMD (windows shell): same
>> error with both of them.
>>
>> Then i tried to write a little script (just the "require" on my lib) and
>> then , launched it directly via "windows explorer". I've got an error of a
>> missing dll this time.
>> So... the answer after a few googling:
>>
>> with mingw, compile it with flag "-static-libgcc"
>>
>> No idea how to manage this with Visual Studio.
>>
>> Now the "require" work , whatever environment: MSYS, CMD, and "windows
>> explorer".
>>
>> Windows mysteries :/
>
> A very useful tool to debug such DLL problems is Dependency Walker
> [1]. It can tell you what dependencies a DLL has (and it displays
> warning if it's unable to find them). It is also useful to examine
> exported symbols, and check that your luaopen_foo is properly named
> and not mangled.
>
> And to find out why -static-libgcc solves the problem, and how to
> solve it with MSVC, you will have to provide more details, such as
> what DLL exactly is reported missing, what unusual things you might
> have in your PATH, or whether you may have several foo.dll files in
> the PATH (for example one for the C/C++ library, and one for the Lua/C
> binding module).
>
> [1] http://www.dependencywalker.com/
>
>



--
Michael Gogins
Irreducible Productions
http://www.michael-gogins.com
Michael dot Gogins at gmail dot com

Reply | Threaded
Open this post in threaded view
|

Re: 回复: Re: Problem calling c++ from Lua (MS-Windows version)

jseb
In reply to this post by Jerome Vuarand
> A very useful tool to debug such DLL problems is Dependency Walker
> [1]. It can tell you what dependencies a DLL has (and it displays
> warning if it's unable to find them). It is also useful to examine
> exported symbols, and check that your luaopen_foo is properly named
> and not mangled.
>
> And to find out why -static-libgcc solves the problem, and how to
> solve it with MSVC, you will have to provide more details, such as
> what DLL exactly is reported missing, what unusual things you might
> have in your PATH, or whether you may have several foo.dll files in
> the PATH (for example one for the C/C++ library, and one for the Lua/C
> binding module).
>
> [1] http://www.dependencywalker.com/


my %PATH% was "classic", nothing strange in it (i checked it with

echo $PATH | awk 'BEGIN { FS=":"} {for (i=1; i<NF; i++) print $i}'

(sorry i don't know how to do it fast with Lua) )

I've got only one "foo.dll". I didn't verify if windows installed one in
its system-path, but i don't think so.

I checked name mangle, which was ok with "objdump" (or "nm", don't
remember). Anyway, compiling with static version of gcc standard lib
solves the issue, so no name mangling here.

And well i do know why i had to add "-static-libgcc".
I wrote the short version, as the story seems to raise few interest. For
those who are more interested, here's the full story

<found on a forum>
All MinGW executables that uses C/C++ routines, classes and libs are
necessary linked against libgcc_s_dwxx-x.dll (GNU C lib) and
libstdc++-xx.dll (GNU C++ STD lib) All these .dll can be found at the
compiler's /bin directory.
All you have to do is to copy&paste those into your main executable's path.
For a static link version of this libs, you can add "-static-libgcc
-static-libstdc++" to your compiler flags and you won't need this .dll
anymore.
Your executable will be dramatically heavier, due to the internal
addition of their contents, but it should be OK.
</found on a forum>


Now, the mystery is:
Why in shell (CMD or MSYS), during the lua's require, i got this error:

 > require "libfoo"
error loading module 'libfoo' from file '.\libfoo.dll':
        Le module spécifié est introuvable.
        (above mean that "specified module could'nt be found)

Ok.
But why when i launch the lua script from explorer (in my case "total
commander", but that's the same from explorer) i got another error ?
This time, the error was about the missing libgcc_s...dll

I can understand that i don't have problems with MSYS, but the same
error (that is, missing GNU libc++) should raise with CMD, as it has
nothing to do with GNU C++ standard lib.

What is missing is something like "strace" under windows, which could
help to solve this sort of issues (Lua probably hang on this, and report
a very generic error message).
So i will give a try for dependency-walker, next time i have a
MS-windows in the neighborhood (no windows here for trying, sorry).

I gave a try with MSVC for trying to figure it out, but i'd rather like
using GNU's. That's odd that MSVC couldn't compile properly my lib, and
perhaps it's another problem (i can't believe that Microsoft C++
standard library is missing in system32 :p ).


Thanks for the link, i guess it will be useful next time !


Reply | Threaded
Open this post in threaded view
|

Re: ????????? Re: Problem calling c++ from Lua (MS-Windows version)

Luiz Henrique de Figueiredo
> echo $PATH | awk 'BEGIN { FS=":"} {for (i=1; i<NF; i++) print $i}'
>
> (sorry i don't know how to do it fast with Lua) )

for w in (os.getenv("PATH")..":"):gmatch(".-:") do print(w) end

Reply | Threaded
Open this post in threaded view
|

Re: ????????? Re: Problem calling c++ from Lua (MS-Windows version)

Norbert Kiesel
On Thu, 2011-02-24 at 16:56 -0300, Luiz Henrique de Figueiredo wrote:
> for w in (os.getenv("PATH")..":"):gmatch(".-:") do print(w) end

for w in (os.getenv("PATH")):gmatch("[^:]+") do print(w) end

avoids the : at the end of path names (and the need to stick an
additional : at the end)

</nk>



Reply | Threaded
Open this post in threaded view
|

Re: 回复: Re: Problem calling c++ from Lua (MS-Windows version)

Michael Gogins
In reply to this post by jseb
Perhaps this indicates a malformed package.cpath or LUA_CPATH
environment variable?

Regards,
Mike

On Thu, Feb 24, 2011 at 2:34 PM, jseb <[hidden email]> wrote:

>> A very useful tool to debug such DLL problems is Dependency Walker
>> [1]. It can tell you what dependencies a DLL has (and it displays
>> warning if it's unable to find them). It is also useful to examine
>> exported symbols, and check that your luaopen_foo is properly named
>> and not mangled.
>>
>> And to find out why -static-libgcc solves the problem, and how to
>> solve it with MSVC, you will have to provide more details, such as
>> what DLL exactly is reported missing, what unusual things you might
>> have in your PATH, or whether you may have several foo.dll files in
>> the PATH (for example one for the C/C++ library, and one for the Lua/C
>> binding module).
>>
>> [1] http://www.dependencywalker.com/
>
>
> my %PATH% was "classic", nothing strange in it (i checked it with
>
> echo $PATH | awk 'BEGIN { FS=":"} {for (i=1; i<NF; i++) print $i}'
>
> (sorry i don't know how to do it fast with Lua) )
>
> I've got only one "foo.dll". I didn't verify if windows installed one in its
> system-path, but i don't think so.
>
> I checked name mangle, which was ok with "objdump" (or "nm", don't
> remember). Anyway, compiling with static version of gcc standard lib solves
> the issue, so no name mangling here.
>
> And well i do know why i had to add "-static-libgcc".
> I wrote the short version, as the story seems to raise few interest. For
> those who are more interested, here's the full story
>
> <found on a forum>
> All MinGW executables that uses C/C++ routines, classes and libs are
> necessary linked against libgcc_s_dwxx-x.dll (GNU C lib) and
> libstdc++-xx.dll (GNU C++ STD lib) All these .dll can be found at the
> compiler's /bin directory.
> All you have to do is to copy&paste those into your main executable's path.
> For a static link version of this libs, you can add "-static-libgcc
> -static-libstdc++" to your compiler flags and you won't need this .dll
> anymore.
> Your executable will be dramatically heavier, due to the internal addition
> of their contents, but it should be OK.
> </found on a forum>
>
>
> Now, the mystery is:
> Why in shell (CMD or MSYS), during the lua's require, i got this error:
>
>> require "libfoo"
> error loading module 'libfoo' from file '.\libfoo.dll':
>       Le module spécifié est introuvable.
>       (above mean that "specified module could'nt be found)
>
> Ok.
> But why when i launch the lua script from explorer (in my case "total
> commander", but that's the same from explorer) i got another error ?
> This time, the error was about the missing libgcc_s...dll
>
> I can understand that i don't have problems with MSYS, but the same error
> (that is, missing GNU libc++) should raise with CMD, as it has nothing to do
> with GNU C++ standard lib.
>
> What is missing is something like "strace" under windows, which could help
> to solve this sort of issues (Lua probably hang on this, and report a very
> generic error message).
> So i will give a try for dependency-walker, next time i have a MS-windows in
> the neighborhood (no windows here for trying, sorry).
>
> I gave a try with MSVC for trying to figure it out, but i'd rather like
> using GNU's. That's odd that MSVC couldn't compile properly my lib, and
> perhaps it's another problem (i can't believe that Microsoft C++ standard
> library is missing in system32 :p ).
>
>
> Thanks for the link, i guess it will be useful next time !
>
>
>



--
Michael Gogins
Irreducible Productions
http://www.michael-gogins.com
Michael dot Gogins at gmail dot com