Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8221951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:06:42+00:00 2026-06-07T14:06:42+00:00

Issue I want to call a Lua script which require() s lyaml module, Lua

  • 0

Issue

I want to call a Lua script which require()s lyaml module, Lua binding for LibYAML, from a C program.

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 github.

The Lua script follows, it works either with Lua 5.1 and 5.2:

-- 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() ) -> 4

Problem

I wrote a C program which should call hello() from the script, following Programming in Lua, Chapter 25 and Lua 5.2 Reference Manual.

The C program follows:

/* foo.c */
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

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;
}

I compile issuing:

gcc -Wall -o foo foo.c -ldl -lm -llua

Then when running foo, I receive at runtime the following error:

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

So I tried to load lyaml from the C program, adding the following line after luaL_openlibs() call:

luaL_requiref(L, "lyaml", luaopen_package, 1);

After recompilation the error becomes:

PANIC: unprotected error in call tu Lua API (
    error running script: 
       hello.lua:4: attempt to index global 'lyaml' (a nil value))
Aborted

So I imagine that there’s no lyaml symbol and the require() call fails somehow.

By reading luaL_requiref() documentation I thought modname would be set by its call setting glb flag to true:

void luaL_requiref (lua_State *L, const char *modname, 
                    lua_CFunction openf, int glb);

Calls function openf with string modname as an argument and sets the call result in package.loaded[modname], as if that function has been called through require.

If glb is true, also stores the result into global modname.
Leaves a copy of that result on the stack.

I tried to comment the require() call in Lua script and the result is the same.

Question

What did I do wrong? Am I forgetting to do something?


EDIT

I hacked the module updating deprecated (removed) functions/types with their substitute as follows:

lua_strlen() -> luaL_len()
luaL_reg     -> luaL_Reg
luaL_getn()  -> luaL_len()

However Lua scripts using lyaml work so I think the problem is not my hack.

I tried original lyaml module with Lua 5.1. Results are the same, so I’m sure the problem is not my hack.

UPDATE

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.

lyaml = require('lyaml')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T14:06:43+00:00Added an answer on June 7, 2026 at 2:06 pm

    foo.lua as written will only run in Lua 5.1.x — your hacked lyaml.c doesn’t set the global lyaml and neither does require in Lua 5.2. I suspect your PATH did not have Lua 5.2 when you ran the first test “works like a charm,” or else you set lyaml manually before you loaded foo.lua.

    foo.lua should start with

    lyaml = require('lyaml')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to call Program1 from Program2 with exact same parameters which I called
This is a very naive issue. I want to call two different class from
I want to call a overridden method from a base class method called by
I have called a view from another with Html.Action method. I want to call
I want to call a C subroutine from Java. I'm using JNI. I have
If I want to call a server function from JavaScript to retrieve a name
I'm having the following issue - I want to traverse all the xml files
I want to issue multiple mysql commands with one mysql_query function. This is my
I want to issue a series of Command executions, but only when the prior
I want to solve this issue in query @ for Ex : id ip

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.