title: Lua 5.2 C API and require
body: <h1>Issue</h1>
  
  <p>I want to call a Lua script which <code>require()</code>s <code>lyaml</code> module, Lua binding for LibYAML, from a C program.</p>
  
  <p>I compiled Lua 5.2 from source and I hacked the module to make it work with Lua 5.2. It can be found on <a href="https://github.com/crohn/yaml" rel="nofollow">github</a>.</p>
  
  <p>The Lua script follows, it works either with Lua 5.1 and 5.2:</p>
  
  <pre><code>-- foo.lua
  require('lyaml')
  
  function hello ()
    res = lyaml.load("a: 4\n")
    return res.a
  end
  
  -- then calling hello() it works like a charm
  print( hello() ) -&gt; 4
  </code></pre>
  
  <p><br /></p>
  
  <h2>Problem</h2>
  
  <p>I wrote a C program which should call <code>hello()</code> from the script, following <a href="http://www.lua.org/pil/25.html" rel="nofollow">Programming in Lua, Chapter 25</a> and <a href="http://www.lua.org/manual/5.2/manual.html" rel="nofollow">Lua 5.2 Reference Manual</a>.</p>
  
  <p>The C program follows:</p>
  
  <pre><code>/* foo.c */
  #include &lt;lua.h&gt;
  #include &lt;lualib.h&gt;
  #include &lt;lauxlib.h&gt;
  
  int main(void)
  {
    double z;
  
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
  
    if (luaL_dofile(L, "foo.lua"))
      luaL_error(L, "error running script: %s", lua_tostring(L, -1));
  
    lua_getglobal(L, "hello");
  
    if (lua_pcall(L, 0, 1, 0) != 0)
      luaL_error(L, "error calling hello: %s", lua_tostring(L, -1));
  
    if (!lua_isnumber(L, -1))
      luaL_error(L, "result must be number");        
  
    z = lua_tonumber(L, -1);
    lua_pop(L, 1);
  
    lua_close(L);
    return 0;
  }
  </code></pre>
  
  <p>I compile issuing:</p>
  
  <pre><code>gcc -Wall -o foo foo.c -ldl -lm -llua
  </code></pre>
  
  <p>Then when running <code>foo</code>, I receive at runtime the following error:</p>
  
  <pre><code>PANIC: unprotected error in call tu Lua API (
      error running script: error loading module 'lyaml' from file '/path/to/lyaml.so':
         /path/to/lyaml.so: undefined symbol: lua_gettop)
  Aborted
  </code></pre>
  
  <p>So I tried to load <code>lyaml</code> from the C program, adding the following line after <code>luaL_openlibs()</code> call:</p>
  
  <pre><code>luaL_requiref(L, "lyaml", luaopen_package, 1);
  </code></pre>
  
  <p>After recompilation the error becomes:</p>
  
  <pre><code>PANIC: unprotected error in call tu Lua API (
      error running script: 
         hello.lua:4: attempt to index global 'lyaml' (a nil value))
  Aborted
  </code></pre>
  
  <p>So I imagine that there's no <code>lyaml</code> symbol and the <code>require()</code> call fails somehow.</p>
  
  <p>By reading <code>luaL_requiref()</code> documentation I thought <code>modname</code> would be set by its call setting <code>glb</code> flag to true:</p>
  
  <pre><code>void luaL_requiref (lua_State *L, const char *modname, 
                      lua_CFunction openf, int glb);
  </code></pre>
  
  <blockquote>
    <p>Calls function <code>openf</code> with string <code>modname</code> as an argument and sets the call result in <code>package.loaded[modname]</code>, as if that function has been called through <code>require</code>.<br /><br />
    If glb is true, also stores the result into global <code>modname</code>.<br />
    Leaves a copy of that result on the stack.</p>
  </blockquote>
  
  <p>I tried to comment the <code>require()</code> call in Lua script and the result is the same.</p>
  
  <h2>Question</h2>
  
  <p>What did I do wrong? Am I forgetting to do something?</p>
  
  <hr>
  
  <h3>EDIT</h3>
  
  <p>I hacked the module updating deprecated (removed) functions/types with their substitute as follows:</p>
  
  <pre><code>lua_strlen() -&gt; luaL_len()
  luaL_reg     -&gt; luaL_Reg
  luaL_getn()  -&gt; luaL_len()
  </code></pre>
  
  <p>However Lua scripts using <code>lyaml</code> work so I think the problem is not my hack.</p>
  
  <p>I tried original <code>lyaml</code> module with Lua 5.1. Results are the same, so I'm sure the problem is not my hack.</p>
  
  <h3>UPDATE</h3>
  
  <p>Adding the following line, as suggested by Doug Currie in his answer, the C program works perfectly with Lua 5.1. I still get the same error in 5.2 though.</p>
  
  <pre><code>lyaml = require('lyaml')
  </code></pre>