Extend Lua with custom C functions using Clang
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
#include <lua.h> #include <lauxlib.h> 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
require "nativefunc" print(nativelib.mult50(50))
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
Other notes
- plan9 Compile drawterm on Fedora 38
- c Extend Lua with custom C functions using Clang
- plan9 Install Plan9port on Linux
- Fix screen tearing on Debian 12 Xorg and i3
- Bulk thumbnails
- 60's IBM Computers Commercial
- Change permissions of matching files recursively
- Cache busting in Hugo
- vim Personal sane Vim defaults
- dcss Make DCSS playable on 4k displays
- plan9 My brand new Plan9/9front desktop
- dcss Dungeon Crawl Stone Soup - New player guide
- Previews how man page written in Troff will look like
- Online radio streaming with MPV from terminal
- c Calling assembly functions from C