GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to use Lua with C.

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code

Did you find these code snippets usefull?
Yes
50%
 50%  [ 2 ]
No
50%
 50%  [ 2 ]
Total Votes : 4

Author Message
caracal
GTK+ Guru


Joined: 21 Jun 2007
Posts: 207
Location: Wilkes Barre Pa

PostPosted: Thu Oct 23, 2008 5:57 pm    Post subject: How to use Lua with C. Reply with quote

About Lua
Lua is the most popular scripting language for game development, has been described as the de facto standard for game scripting, and is the language of Baldur's Gate, Escape from Monkey Island, FarCry, Grim Fandango, Homeworld 2, Illarion, Impossible Creatures, Psychonauts, The Sims, and World of Warcraft. Adobe, Disney, Electronic Arts, Intel, LucasArts, Microsoft, NASA, Olivetti, and Philips all use Lua heavily.

How to call a C Function from Lua

luaavg.c
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

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

/* gcc -o luaavg luaavg.c -I/usr/local/include -L/usr/local/lib -llua -lm -ldl */

/* the Lua interpreter */
lua_State* L;

static int average(lua_State *L)
{
    /* get number of arguments */
   
int n = lua_gettop(L);
    double sum = 0;
    int i;

    /* loop through each argument */
   
for (i = 1; i <= n; i++)
    {
        /* total the arguments */
       
sum += lua_tonumber(L, i);
    }

    /* push the average */
   
lua_pushnumber(L, sum / n);

    /* push the sum */
   
lua_pushnumber(L, sum);

    /* return the number of results */
   
return 2;
}

int main ( int argc, char *argv[] )
{
    /* initialize Lua */
   
L = lua_open();

    /* load Lua base libraries */
   
luaL_openlibs(L);

    /* register our function */
   
lua_register(L, "average", average);

    /* run the script */
   
luaL_dofile(L, "avg.lua");

    /* cleanup Lua */
   
lua_close(L);

    /* pause */
   
printf( "Press enter to exit..." );

    getchar();

    return 0;
}



avg.lua
Code: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11

-- call a C function

avg, sum = average(10, 20, 30, 40, 50)
print("The average is ", avg)
print("The sum is ", sum)

avg, sum = average(1, 2, 3, 4, 5)
print("The average is ", avg)
print("The sum is ", sum)



How to call a Lua Function from C

luadd.c
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

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

/* gcc -o luadd luadd.c -I/usr/local/include -L/usr/local/lib -llua -lm -ldl */

/* the Lua interpreter */
lua_State* L;

int luaadd ( int x, int y )
{
    int sum;

    /* the function name */
   
lua_getglobal(L, "add");

    /* the first argument */
   
lua_pushnumber(L, x);

    /* the second argument */
   
lua_pushnumber(L, y);

    /* call the function with 2 arguments, return 1 result */

   
lua_call(L, 2, 1);

    /* get the result */
   
sum = (int)lua_tointeger(L, -1);

    lua_pop(L, 1);

    return sum;
}

int main ( int argc, char *argv[] )
{
    int sum;

    /* initialize Lua */
   
L = lua_open();

    /* load Lua base libraries */
   
luaL_openlibs(L);

    /* load the script */
   
luaL_dofile(L, "add.lua");

    /* call the add function */
   
sum = luaadd( 10, 15 );

    /* print the result */
   
printf( "The sum is %d\n", sum );

    /* cleanup Lua */
   
lua_close(L);

    /* pause */
   
printf( "Press enter to exit..." );

    getchar();

    return 0;
}


add.lua
Code: (Plaintext)
1
2
3
4
5
6

-- add two numbers
function add ( x, y )
    return x + y
end



How to execute a Lua Script from C.

luatest.c
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

/* gcc -o luatest luatest.c -I/usr/local/include -L/usr/local/lib -llua -lm -ldl */

int main()
{
    int s=0;
    lua_State *L = lua_open();

    // load the libs
   
luaL_openlibs(L);

    //run a Lua scrip here
   
luaL_dofile(L,"foo.lua");

    printf("\nAllright we are back in C.\n");

    lua_close(L);

    return 0;
}


foo.lua
Code: (Plaintext)
1
2
3
4
5

io.write("Please enter your name: ")
name = io.read() -- read input from user
print ("Hi " .. name .. ", did you know we are in lua right now?")



How to get a Varible value from Lua to C.

test.c
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

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

/* gcc -o test test.c -I/usr/local/include -L/usr/local/lib -llua -lm -ldl */

int main()
{
    lua_State *L = lua_open();
   
    if(luaL_loadfile(L,"settings.lua") || lua_pcall(L,0,0,0))
        printf("Error failed to load %s",lua_tostring(L,-1));
    else
   
{
           
        lua_getglobal(L,"screenWidth");
            const int screenWidth = lua_tonumber(L,-1);
            printf("Screen Width = %d \n", screenWidth);
           
        lua_getglobal(L,"appName");
            const char *appName = luaL_checkstring(L, -1);     
            printf("Screen Name = %s \n", appName);

    }
   
    lua_close(L);

    /* If we got this far, everything worked */
   
printf("Success!\n");

    return 0;
}


settings.lua
Code: (Plaintext)
1
2
3
4

appName = "Firefox"
screenWidth = 400


How to make a Lua Module for a C function using Cinvoke.

libtest.c
Code: (C)
1
2
3
4
5
6

int sum(int a, int b)
{
    return (a+b);
}


gcc -fPIC -c libtest.c
gcc -shared libtest.o -o libtest.so

echo $HOME
mkdir $HOME/example

cp libtest.so $HOME/example
export LD_LIBRARY_PATH=$HOME/example

Code: (Plaintext)
1
2
3
4
5
6
7

require("cinvoke_lua")

libtest = clibrary.new("libtest.so")
sum = libtest:get_function(Cint, "sum", Cint, Cint)
print(sum(5, 10))


How to make a Lua Module for a C function using Swig.

How to Build Dynamic Module:
ath.c
Code: (C)
1
2
3
4
5
6
7
8

#include <stdio.h>
#include "ath.h"

int add(int x, int y) {
    return x+y;
}


ath.h
Code: (Plaintext)
1
extern int add(int x, int y);


ath.i
Code: (Plaintext)
1
2
3
4
5
6
7
8

/* ath.i */
%module ath
%{
       extern int add(int x, int y);
%}
       extern int add(int x, int y);


Build:
Code: (Plaintext)
1
2
3
4
5
6

swig -lua ath.i
gcc -I/usr/include/lua -c ath_wrap.c -o ath_wrap.o -llua -lm -ldl
gcc -c ath.c -o ath.o
gcc ­shared ­I/usr/include/lua ­L/usr/lib/lua ath_wrap.o ath.o ­o ath.so -llua -lm -ldl


Usage Example:
Code: (Plaintext)
1
2
3
4

require("ath")
print(ath.add(10, 80))


or

How to Build Statically Linked with Swig:

ath.c
Code: (Plaintext)
1
2
3
4
5
6
7
8

#include <stdio.h>
#include "ath.h"

int add(int x, int y) {
    return x+y;
}


ath.h
Code: (Plaintext)
1
extern int add(int x, int y);


ath.i
Code: (Plaintext)
1
2
3
4
5
6
7
8

/* ath.i */
%module ath
%{
       extern int add(int x, int y);
%}
       extern int add(int x, int y);


min.c
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#include <stdio.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "ath.h"

extern int luaopen_ath(lua_State* L); // declare the wrapped module

int main(int argc,char* argv[]) {
   
 lua_State *L;
 
 if (argc<2)
 {
     printf("%s: <filename.lua>\n",argv[0]);
     return 0;
 }
 
 L=lua_open();
 luaopen_base(L);    // load basic libs (eg. print)
 
luaopen_ath(L);    // load the wrappered module
 
 
if (luaL_loadfile(L,argv[1])==0) {
     lua_pcall(L,0,0,0);
 }

 return 0;
}


test.lua
Code: (Plaintext)
1
2
3

print(ath.add(10, 80))


Build:
Code: (Plaintext)
1
2
3
4
5
6
7

swig -lua ath.i
gcc -I/usr/include/lua -c min.c -o min.o
gcc -I/usr/include/lua -c ath_wrap.c -o ath_wrap.o -llua -lm -ldl
gcc -c ath.c -o ath.o
gcc -I/usr/include/lua  -L/usr/lib/lua min.o ath.o ath_wrap.o -o my_lua -llua -lm -ldl


Usage Example:
Code: (Plaintext)
1
2
3

./my_lua test.lua


Please report any broken code so i can fix it.
Thanks Caracal
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP