From 1100562e29f6476448b656dbddd4cf22505523f6 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Sun, 10 Mar 2024 14:59:14 +0100 Subject: Move back to JBMAFP --- .../notes/2023-05-23-extend-lua-with-custom-c.md | 55 ---------------------- 1 file changed, 55 deletions(-) delete mode 100644 _posts/notes/2023-05-23-extend-lua-with-custom-c.md (limited to '_posts/notes/2023-05-23-extend-lua-with-custom-c.md') diff --git a/_posts/notes/2023-05-23-extend-lua-with-custom-c.md b/_posts/notes/2023-05-23-extend-lua-with-custom-c.md deleted file mode 100644 index 604d359..0000000 --- a/_posts/notes/2023-05-23-extend-lua-with-custom-c.md +++ /dev/null @@ -1,55 +0,0 @@ ---- -title: Extend Lua with custom C functions using Clang -permalink: /extend-lua-with-custom-c.html -date: 2023-05-23T12:00:00+02:00 -layout: post -type: note -draft: false -tags: [lua, c] ---- - -Here is a boilerplate for extending Lua with custom C functions. This requires -Clang and Lua 5.1 to be installed. GCC can be used instead of Clang, but the -Makefile will need to be modified. - -- nativefunc.c - - ```c - #include - #include - - static int l_mult50(lua_State *L) { - double number = luaL_checknumber(L, 1); - lua_pushnumber(L, number * 50); - return 1; - } - - int luaopen_nativefunc(lua_State *L) { - static const struct luaL_Reg nativeFuncLib[] = { {"mult50", l_mult50}, {NULL, NULL} }; - - luaL_register(L, "nativelib", nativeFuncLib); - return 1; - } - ``` - -- main.lua - - ```lua - require "nativefunc" - print(nativelib.mult50(50)) - ``` - -- Makefile - - ```Makefile - CC = clang - CFLAGS = - INCLUDES = `pkg-config lua5.1 --cflags-only-I` - - all: - $(CC) -shared -o nativefunc.so -fPIC nativefunc.c $(CFLAGS) $(INCLUDES) - - clean: - rm *.so - ``` - -- cgit v1.2.3