aboutsummaryrefslogtreecommitdiff
path: root/content/notes/2023-05-23-extend-lua-with-custom-c.md
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2023-11-01 22:54:27 +0100
committerMitja Felicijan <mitja.felicijan@gmail.com>2023-11-01 22:54:27 +0100
commit2417a6b7603524dc5cd30d29b153f91024b9443d (patch)
tree9be5ea8e5baba96dd9159217da6badf6157fb595 /content/notes/2023-05-23-extend-lua-with-custom-c.md
parent89ba3497f07a8ea43d209b583f39fcc286acc923 (diff)
downloadmitjafelicijan.com-2417a6b7603524dc5cd30d29b153f91024b9443d.tar.gz
Move to Jekyll
Diffstat (limited to 'content/notes/2023-05-23-extend-lua-with-custom-c.md')
-rw-r--r--content/notes/2023-05-23-extend-lua-with-custom-c.md54
1 files changed, 0 insertions, 54 deletions
diff --git a/content/notes/2023-05-23-extend-lua-with-custom-c.md b/content/notes/2023-05-23-extend-lua-with-custom-c.md
deleted file mode 100644
index 554a6a4..0000000
--- a/content/notes/2023-05-23-extend-lua-with-custom-c.md
+++ /dev/null
@@ -1,54 +0,0 @@
1---
2title: Extend Lua with custom C functions using Clang
3url: extend-lua-with-custom-c.html
4date: 2023-05-23T12:00:00+02:00
5type: note
6draft: false
7tags: [lua, c]
8---
9
10Here is a boilerplate for extending Lua with custom C functions. This requires
11Clang and Lua 5.1 to be installed. GCC can be used instead of Clang, but the
12Makefile will need to be modified.
13
14- nativefunc.c
15
16 ```c
17 #include <lua.h>
18 #include <lauxlib.h>
19
20 static int l_mult50(lua_State *L) {
21 double number = luaL_checknumber(L, 1);
22 lua_pushnumber(L, number * 50);
23 return 1;
24 }
25
26 int luaopen_nativefunc(lua_State *L) {
27 static const struct luaL_Reg nativeFuncLib[] = {{"mult50", l_mult50}, {NULL, NULL}};
28
29 luaL_register(L, "nativelib", nativeFuncLib);
30 return 1;
31 }
32 ```
33
34- main.lua
35
36 ```lua
37 require "nativefunc"
38 print(nativelib.mult50(50))
39 ```
40
41- Makefile
42
43 ```Makefile
44 CC = clang
45 CFLAGS =
46 INCLUDES = `pkg-config lua5.1 --cflags-only-I`
47
48 all:
49 $(CC) -shared -o nativefunc.so -fPIC nativefunc.c $(CFLAGS) $(INCLUDES)
50
51 clean:
52 rm *.so
53 ```
54