tags:

views:

130

answers:

3

I'm just now diving into SWIG as a means for creating Lua bindings, and I've hit a snag. I made my interface file and built a shared object file from it all without any problems. However, when I run Lua and try to require the shared object, I get the following:

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
 require("my_example")
 error loading module 'my_example' from file './my_example.so':

        ./my_example.so: undefined symbol: luaopen_my_example

stack traceback:

[C]: ?

[C]: in function 'require'

stdin:1: in main chunk

[C]: ?

I can't seem to find anything online describing this situation, so I thought I'd post here. Are there any SWIG Gurus out there? The class I'm wrapping is a few thousand lines long (and proprietary) or I'd post it here.

Thanks! :D

+1  A: 

What version of SWIG are you using and on what platform?

The slightly stale version 1.3.29 of SWIG that I ran under Cygwin uses #define SWIG_init buried deep in its generated wrapper file to specify the name wanted by require as luaopen_mod where mod was the name used in the %module statement at the top of the SWIG .i file. The module I wrapped does load and work with Lua 5.1 on Windows.

You might also need to do something to make sure that the symbol is exported, and wasn't name-mangled to a C++ name that will never be found by Lua. I was wrapping C structs and part of the Windows API, so didn't deal with any C++ specific issues in that wrapper. However, accidental name-mangling is a frequent cause of module loading issues even without SWIG involved.

RBerteig
A: 

I'm using Swig Version 1.3.39 on CentOS Linux.

Do you suggest I examine the modulename.wrap file swig produced to see if the code with the #define SWIG_init section got mangled, somehow?

What flags should I compile with to make sure the symbol tables are exported properly?

Thanks for your help, RBerteig!

A: 

/facepalm

I had the module misnamed in the interface (.i) file. Thanks anyway!

Zack