C API and static functions

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

C API and static functions

Matt Eisan

I noticed most, if not all, examples of programs that involve writing modules for Lua define all C functions as static. Why is this and why isbot necessary?

Reply | Threaded
Open this post in threaded view
|

Re: C API and static functions

Choonster TheMage
On 10 June 2013 06:47, Matt Eisan <[hidden email]> wrote:
> I noticed most, if not all, examples of programs that involve writing
> modules for Lua define all C functions as static. Why is this and why isbot
> necessary?

static functions are only visible within their own file. Non-static
functions can be called from outside of their own file.

http://stackoverflow.com/questions/5319361/static-function-in-c
http://stackoverflow.com/questions/1665250/why-declare-a-variable-or-function-static-in-c

In most Lua modules, the only function you want to export to outside
code is your luaopen_xxx function (which Lua needs to be able to call
to load your module). The other functions are generally only intended
for use by your own module or from within Lua.

Reply | Threaded
Open this post in threaded view
|

Re: C API and static functions

Sean Conner
In reply to this post by Matt Eisan
It was thus said that the Great Matt Eisan once stated:
> I noticed most, if not all, examples of programs that involve writing
> modules for Lua define all C functions as static. Why is this and why isbot
> necessary?

  In C, a fuction declared as static:

        static int mymath_randomseed(lua_State *L);

will only be visible (i.e. usabled) by functions in the same file.  In other
words, it has what is called "file scope".  Once compiled, code outside the
file cannot see or call mymath_randomseed().  One result is that the static
function is hidden from code in other files.

  Another result is that the function is invisble to the linker (the code
that takes the output of the compiler and produces an executable) and in
some cases, this does have an impact.  One impact is the size of the
resulting program (for dynamically linked programs---for statically linked
programs the size doesn't matter).  Every non-static function increases the
size of the executable, since information about the function (name, address,
etc) needs to be stored and made available.

  Now, for the case of Lua modules, it's not necessary to define the C
functions as static---the module will most likely work.  But, if it defines
a non-static function used internally, say, foo(), and another module is
loaded that also defines a non-static function named foo(), the second
module will fail to load (or if it does, the program stands a good chance of
crashing).

  And if you think this shouldn't happen, it does.  I just checked a few Lua
modules, and the following modules:

                posix
                lzlib
                gdbm

  defined the following function:

                pushresults()

  Fortunately, all the definitions were defined as "static" so there are no
conflicts.

  -spc


Reply | Threaded
Open this post in threaded view
|

Re: C API and static functions

Matt Eisan

oh, that makes perfect sense. thank you both for your detailed responses, much appreciated.