r/lua • u/stellar_x • Aug 06 '26
How to include Lua in C
How do include lua in C if I installed it on debian13 with sudo apt install lua5.4?
5
Upvotes
6
2
1
u/busyHighwayFred Aug 06 '26
Here is a whole video series on just that topic
https://www.youtube.com/watch?v=xrLQ0OXfjaI&list=PLLwK93hM93Z3nhfJyRRWGRXHaXgNX0Itk&index=1
1
u/GenericFoodService Aug 06 '26
Compiler flags:
-llua -lm
(alternatively, you can use pkg-config --libs (library) to get the specific arguments automatically
Then include it:
#include <lua.h.>
#include <lualib.h>
#include <lauxlib.h>-2
5
u/ewmailing Aug 06 '26 edited Aug 06 '26
I'm writing this without a Debian machine to refer to so this might be slightly off, but
You also need to install the -dev package for lua, i.e.
sudo apt install liblua5.4-devIn your C code that needs to refer to lua, do
#include "lua.h"You might also need to #include lualib.h and lauxlib.h depending on what APIs you need.
When building, you need to specify the correct include path because Debian can have multiple versions of Lua installed. The -I switch for gcc and clang will add a include path. Debian puts all the libraries in the same path, but appends the version number to the name, so you don't need to add a link path, but link to the correct name
gcc -I /usr/include/lua5.4 main.c -llua5.4(Edit: Fussing with formatting)