Skip to content

Instantly share code, notes, and snippets.

@jarednorman
Created February 22, 2016 08:07
Show Gist options
  • Save jarednorman/ce3ddfcaf1f337146d3a to your computer and use it in GitHub Desktop.
Save jarednorman/ce3ddfcaf1f337146d3a to your computer and use it in GitHub Desktop.
Setting up a Lua/C Application

Setting up a Lua/C Application

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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment