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 8388223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:25:34+00:00 2026-06-09T18:25:34+00:00

I have a c program running embedded Lua. as of right now, it’s just

  • 0

I have a c program running embedded Lua. as of right now, it’s just a hello world. before moving on, though, I’d like to be able to get the lua output to be sent somewhere other than stdout, so that I can manipulate it in some manner. Here’s my code:

#include <stdio.h>

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

int main() {
    lua_State *luaVM = luaL_newstate();
    //char result[1024];

    if (luaVM == NULL) {
        printf("Error initializing lua!\n");
        return -1;
    }
    luaL_openlibs(luaVM);
    luaL_dostring(luaVM, "print(\"hello world!\")");
    //Somehow put the output into result

    //printf("%s\n%s\n", result, result);

    lua_close(luaVM);
    return 0;
}

For example, I’d like to use result, seen in the comments, to print the result of the lua code twice. Can this be done?

  • 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-09T18:25:36+00:00Added an answer on June 9, 2026 at 6:25 pm

    This answer can get critics from some readers, but first please have a look at my blog post which I’ve prepared specially for this answer, and read the foreword why I choose this solution.

    As promised, I’ve upstreamed my old Lua 5.1 output redirection patch to the latest version.
    Patches are available here: 5.1.5 and 5.2.1.

    Patch:

    patch -p1 < ~/download/lua-5.2.1-output-redirect.patch
    

    Build:

    cd src  
    make a LUA_A="liblua-5.2.1-redirect.a" SYSCFLAGS="-DLUA_USE_LINUX -DLUA_REDIRECT" SYSLIBS="-Wl,-E -ldl -lreadline -lncurses"
    

    Check:

    nm liblua-5.x.y-redirect.a | grep printf
    nm liblua-5.x.y-redirect.a | grep fputs
    nm liblua-5.x.y-redirect.a | grep fwrite
    

    Test:

    Obtain test program here (C/C++ mixed, sorry). Build it via:

    g++ -DLUA_REDIRECT -I/path/to/lua-5.2.1/src/ -L. lua-redirect-test.cpp -llua-5.2.1-redirect -ldl -o lua-redirect-test  
    

    Output:

    ===== Test 1 output =====
    Lua stdout buffer:
    ---
    hello world!
    
    ---
    Lua stderr buffer:
    ---
    
    ---
    Lua error message:
    ---
    (null)
    ---
    ===== Test 2 output =====
    Lua stdout buffer:
    ---
    
    ---
    Lua stderr buffer:
    ---
    
    ---
    Lua error message:
    ---
    [string "bad_function()"]:1: attempt to call global 'bad_function' (a nil value)
    ---
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I have my program running on my SQLExpress database. But what I'd
I have a java program that is running in the background(Windows). I would like
I have a Matlab program that is running longer than I'd like it to.
I have a long-running multithreaded program, and I'd like to occasionally like to call
I now have a running Java program which only lacks of the final step,that
I have a program running sometime alright, the other time it just won't. The
I have a program running under WPF and MVVM. I have some daily worked
I have a .NET CF program (running on a Smartphone) and I have written
I have a GLSL shader program running my iPhone app (it's a very very
I have a program in Octave that has a loop - running a function

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.