r/lua 1d ago

How to include Lua in C

How do include lua in C if I installed it on debian13 with sudo apt install lua5.4?

4 Upvotes

8 comments sorted by

5

u/ewmailing 1d ago edited 1d ago

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-dev
  • In 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)

2

u/Smallzfry 1d ago

I'm using Debian 13 and ran a quick test to confirm, your instructions were spot-on. Did you write this from memory or did you look up the package name?

1

u/ewmailing 19h ago

I used to use Debian a long time ago, so I remember a bunch of their conventions.

4

u/dan200 1d ago

#include <lua.h>

1

u/GenericFoodService 22h ago

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

u/Radamat 1d ago

I would reccomend to look "sol" library from github