Setting up a Lua/C Application

Warning: This post is very old. It may be out of date, and links in it may not work. Continue at your own risk.

My first exposure to Lua was in highschool. When I started seriously programming in tenth grade, it was in C (though I’d had some exposure to C++ as early as elementary school.) Being a nerdy teenager, all I wanted to do was make games, and I quickly discovered that doing everything in pure C had some downsides, and that embedding a scripting language within your application was commonplace for reasons that I have no intention of explaining here. I assume you already want to embed Lua in your C application, else why would you be reading this.

I recently tried to get an older application compiling (the source code hadn’t been touched in about ten years) and ran into some difficulty. For posterity, here is a main.c and a Makefile that should get you compiling under any reasonably up-to-date Linux distribution with Lua 5.2 installed.

Makefile

CFLAGS+=-I/usr/include/lua5.2/
LIBS+=-llua5.2
OBJECTS=main.o
OUTPUT=executablename

all: $(OUTPUT)

$(OUTPUT): $(OBJECTS)
    $(CC) -o $@ $^ $(LIBS)

clean :
    $(RM) $(OUTPUT) *.o

main.c

#include <stdio.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

int main(int argc, char *argv[]) {
    lua_State *L = luaL_newstate();
    printf("Hello world.\n");
    return 0;
}

I provide technical leadership, risk assessment, project planning, training, and additional development to software teams. If you need help taking a project from idea through execution, get in touch.