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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:55:15+00:00 2026-05-12T08:55:15+00:00

I’m currently working on implementing Lua into one of the applications that I’m working

  • 0

I’m currently working on implementing Lua into one of the applications that I’m working on. Currently I’m just using the C api and registering functions using lua_register, but I’d like to be able to pass static and non static function pointers to certain class methods.

I’ve found certain libraries on the net, but since I need very little of the overall functionality they provide I was wondering if there’s an easy way to do this.

Thank you.

  • 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-05-12T08:55:16+00:00Added an answer on May 12, 2026 at 8:55 am

    A complex library API can often be wrapped quickly and (nearly) completely using SWIG. An advantage of using SWIG in this case is that it is easy to build SWIG-based wrappers that enable use of the library in 18 major languages including Lua, Perl, Python, Ruby, and Java, among others.

    If Lua is your preferred (and possibly only) concern, then I’d recommend learning to use luaL_register() at the core of a strategy to build Lua modules in C. An advantage of building a module this way is that you keep all of your functions in a single name space without any overhead. You will need to craft a wrapper function that matches the Lua C function calling convention (just as you do with lua_register()) and that gathers the Lua arguments from the stack, calls the wrapped function, and pushes any return value and out parameters back to the Lua stack. A good overview of how to go about this can be found in the book Programming in Lua. The online copy of the first edition discusses library creation in Chapter 26, but was written for Lua 5.0. I strongly urge anyone seriously using Lua to own a copy of the current edition of PiL.

    Unfortunately, one area where Lua 5.1 differs the most from 5.0 is in the dynamic loading of modules (both C and Lua) with require.

    Here’s a complete (if small) example for a C library that works in Lua 5.1. We start with the implementation of the wrapper in a C file:

    #include <lua.h>
    #include <luaxlib.h>
    #include <math.h>
    #undef PI
    #define PI (3.14159265358979323846)
    
    static int l_sin (lua_State *L) {
        double r = luaL_checknumber(L,1);
        lua_pushnumber(L, sin(r));
        return 1;
    }
    
    static int l_cos (lua_State *L) {
        double r = luaL_checknumber(L,1);
        lua_pushnumber(L, cos(r));
        return 1;
    }
    
    static const struct luaL_reg smlib [] = {
        {"sin", l_sin},
        {"cos", l_cos},
        {NULL, NULL}  /* sentinel */
    };
    
    int luaopen_sm (lua_State *L) {
        luaL_openlib(L, "sm", smlib, 0);
        lua_pushnumber(L,PI);
        lua_rawset(L,-2,"pi");
        return 1;
    }
    

    Note in particular that the only function that need be exported is luaopen_sm(), whose name must correspond to the name of the module that will be used with require, and with the name of the DLL file. With that file compiled as a DLL named sm.dll (probably named libsm.so on Unix-like systems), then it can be loaded and used in a Lua script like this:

    require "sm"
    print(sm.sin(sm.pi/3), sm.cos(sm.pi/3));
    

    This example, although untested, should compile and run. For a complete example wrapping most functions from math.h, see the source to the math module that is distributed with Lua. Because these thin wrappers contain a lot of repetitive code, tools like SWIG are often able to create them given only the declaration of each function.

    Wrapping methods of a C++ class is similar in principle. Each Lua-callable wrapper function is going to need an argument that can be mapped into this on the C++ side, and it must be implemented as either a module-static function or static member function that also locates the target object instance as well as converts the other arguments. SWIG is particularly good at constructing this kind of wrapper and hides a lot of gory details along the way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 152k
  • Answers 152k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Why not just use a Binding like this: <Image Source="{Binding… May 12, 2026 at 10:07 am
  • Editorial Team
    Editorial Team added an answer The problem was in the fixture file which was created… May 12, 2026 at 10:07 am
  • Editorial Team
    Editorial Team added an answer textBox1.Text += "new Log Message" + Environment.NewLine; make the texbox… May 12, 2026 at 10:07 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.