title: Undefined symbol when calling from Lua
body: <p>I have a lib, called "test.so" that uses functions from two libraries that reference each other. If I call the functions of test.so within a C program, it works just fine, so I assume there's no error in C functions.</p>
  
  <p>However, when I call it from Lua, it throws a Seg Fault error due to an "undefined symbol". The problem is, the symbol is defined, I can see it when I run nm test.so.</p>
  
  <p>Reading the thread below</p>
  
  <p><a href="https://stackoverflow.com/questions/14616012/lua-c-modules-cant-reference-eachother-undefined-symbol">Lua: C++ modules can&#39;t reference eachother, undefined symbol</a></p>
  
  <p>I tried to creating a new module that would load test.so using dlopen, as described by Rena. </p>
  
  <pre><code>#include &lt;lua.h&gt;                               
  #include &lt;lauxlib.h&gt;                           
  #include &lt;lualib.h&gt;
  
  #include &lt;stdio.h&gt;
  #include &lt;dlfcn.h&gt;
  
  int luaopen_drmaa(lua_State *L){
      printf("Chamou luaopen_test\n");
      if( dlopen("/home/erica/Downloads/test.so", RTLD_NOW | RTLD_GLOBAL) == NULL)
          printf("%s\n", dlerror());
      else
          printf("Chamou dlopen e teve retorno nao null\n");
      return 0;
  }
  </code></pre>
  
  <p>I compiled it:</p>
  
  <pre><code>gcc -g drmaa_lib.c -shared -fpic -I /usr/include/lua5.1 -I /usr/local/include/ -L /usr/local/lib/m -llua5.1 -o drmaa.so
  </code></pre>
  
  <p>But when I run, I get:</p>
  
  <pre><code>$ lua5.1
  Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
  &gt; require "drmaa"
  Chamou luaopen_test
  Chamou dlopen e teve retorno nao null
  &gt; submitJob()
  stdin:1: attempt to call global 'submitJob' (a nil value)
  stack traceback:
      stdin:1: in main chunk
      [C]: ?
  </code></pre>
  
  <p>Then I tried inserting in main function</p>
  
  <pre><code>luaopen_test(L);  
  </code></pre>
  
  <p>and above the main function</p>
  
  <pre><code>extern int luaopen_test(lua_State *L);
  </code></pre>
  
  <p>to actually open the lib, but I get this error:</p>
  
  <pre><code>$ lua5.1
  Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
  &gt; require "drmaa"
  error loading module 'drmaa' from file './drmaa.so':
      ./drmaa.so: undefined symbol: luaopen_test
  stack traceback:
      [C]: ?
      [C]: in function 'require'
      stdin:1: in main chunk
      [C]: ?
  </code></pre>
  
  <p>The author of the referenced question doesn't show details of the development of the solution.
  Does anyone has a clue of what may make it work?</p>
  
  <p>Thanks in advance.</p>