OS X: Leading underscores and symbol name redirection

  • From: Miles <semanticist@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Tue, 21 Aug 2012 22:34:36 -0700

In Mac OS X, C compilers generate symbols that are prefixed with
underscores, such that these two declarations are functionally

    int close(int);
    int close(int) __asm("_close");

Meanwhile, dlsym() automatically prepends an underscore to its argument:

     The symbol name passed to dlsym() is the name used in C source
code.  For example to find the address
     of function foo(), you would pass "foo" as the symbol name. [1]

So the second declaration above, when used with the LuaJIT FFI, will
result in LJ attempting to resolve a non-existent symbol named
"__close" (since it passes the name obtained from __asm directly to

The main impact for me is that OS X's system header files generally
can't be read directly with ffi.cdef (caveats[2] aside), as a
significant number of functions are declared with __asm.



Other related posts: