diff --git a/src/SFMT.c b/src/SFMT.c
index 3191cc56ec8fcea94ee11d43655556b22ca48df1..497e29841c7640033746a58165d12fbe073228df 100644
--- a/src/SFMT.c
+++ b/src/SFMT.c
@@ -593,7 +593,7 @@ void init_by_array(uint32_t init_key[], int key_length) {
 
 uint32_t rand_div(uint32_t m)
 {
-	uint32_t r, n;
+	uint32_t r;
 
 	/* Hack -- simple case */
 	if (m <= 1) return (0);
diff --git a/src/core_lua.c b/src/core_lua.c
index 047f0f571109a0449db07a752ecabcb14a3eafce..0848c2e96bd78852d5412844f241d289102b7b18 100644
--- a/src/core_lua.c
+++ b/src/core_lua.c
@@ -27,10 +27,13 @@
 #include "script.h"
 #include "display.h"
 #include "physfs.h"
+#include "physfsrwops.h"
 #include "SFMT.h"
 #include "mzip.h"
 #include "main.h"
 #include "tSDL.h"
+#include <math.h>
+#include <time.h>
 
 /******************************************************************
  ******************************************************************
@@ -303,7 +306,7 @@ static const struct luaL_reg fovcache_reg[] =
 static int lua_get_mouse(lua_State *L)
 {
 	int x = 0, y = 0;
-	int buttons = SDL_GetMouseState(&x, &y);
+	(void)SDL_GetMouseState(&x, &y);
 
 	lua_pushnumber(L, x);
 	lua_pushnumber(L, y);
@@ -1221,7 +1224,7 @@ static int lua_line_step(lua_State *L)
 
 static int lua_free_line(lua_State *L)
 {
-	line_data *data = (line_data*)auxiliar_checkclass(L, "line{core}", 1);
+	(void)auxiliar_checkclass(L, "line{core}", 1);
 	lua_pushnumber(L, 1);
 	return 1;
 }
@@ -1351,7 +1354,7 @@ static int lua_file_read(lua_State *L)
 static int lua_file_write(lua_State *L)
 {
 	PHYSFS_file **f = (PHYSFS_file**)auxiliar_checkclass(L, "physfs{file}", 1);
-	int len;
+	size_t len;
 	const char *data = lua_tolstring(L, 2, &len);
 
 	PHYSFS_write(*f, data, sizeof(char), len);
@@ -1403,7 +1406,7 @@ static int lua_zip_add(lua_State *L)
 {
 	zipFile *zf = (zipFile*)auxiliar_checkclass(L, "physfs{zip}", 1);
 	const char *filenameinzip = luaL_checkstring(L, 2);
-	int datalen;
+	size_t datalen;
 	const char *data = lua_tolstring(L, 3, &datalen);
 	int opt_compress_level = luaL_optnumber(L, 4, 4);
 
diff --git a/src/main.c b/src/main.c
index 4921c6db19cc8fa28529cabbfd36bcca2ab73898..6041e83ac1854100325f83372252cf0d6402b178 100644
--- a/src/main.c
+++ b/src/main.c
@@ -27,11 +27,14 @@
 #include "lua.h"
 #include "lauxlib.h"
 #include "lualib.h"
+#include "luasocket.h"
+#include "luasocket/mime.h"
 #include "SFMT.h"
 
 #include "types.h"
 #include "script.h"
 #include "physfs.h"
+#include "physfsrwops.h"
 #include "core_lua.h"
 #include "getself.h"
 #include "music.h"
@@ -49,6 +52,15 @@ bool no_sound = FALSE;
 bool isActive = TRUE;
 bool tickPaused = FALSE;
 
+/* Some lua stuff that's external but has no headers */
+int luaopen_mime_core(lua_State *L);
+int luaopen_profiler(lua_State *L);
+int luaopen_lpeg(lua_State *L);
+int luaopen_map(lua_State *L);
+int luaopen_particles(lua_State *L);
+int luaopen_sound(lua_State *L);
+int luaopen_lanes(lua_State *L);
+
 static int traceback (lua_State *L) {
 	lua_Debug ar;
 	int n;
@@ -402,7 +414,7 @@ void do_resize(int w, int h, bool fullscreen)
 	screen = SDL_SetVideoMode(w, h, 32, flags);
 	if (screen==NULL) {
 		printf("error opening screen: %s\n", SDL_GetError());
-		return 0;
+		return;
 	}
 
 	resizeWindow(screen->w, screen->h);
@@ -414,6 +426,9 @@ void do_resize(int w, int h, bool fullscreen)
 
 // Let some platforms use a different entry point
 #ifdef USE_TENGINE_MAIN
+#ifdef main
+#undef main
+#endif
 #define main tengine_main
 #endif
 
@@ -489,7 +504,7 @@ int main(int argc, char *argv[])
 	Uint32 flags=SDL_INIT_VIDEO | SDL_INIT_TIMER;
 	if (SDL_Init (flags) < 0) {
 		printf("cannot initialize SDL: %s\n", SDL_GetError ());
-		return;
+		return -1;
 	}
 
 	SDL_WM_SetIcon(IMG_Load_RW(PHYSFSRWOPS_openRead("/data/gfx/te4-icon.png"), TRUE), NULL);
@@ -498,7 +513,7 @@ int main(int argc, char *argv[])
 	do_resize(WIDTH, HEIGHT, FALSE);
 	if (screen==NULL) {
 		printf("error opening screen: %s\n", SDL_GetError());
-		return;
+		return -1;
 	}
 	SDL_WM_SetCaption("T4Engine", NULL);
 	SDL_EnableUNICODE(TRUE);
diff --git a/src/music.c b/src/music.c
index ed2b00091d19eec20b9813ae4b6c62a218ad6557..5e1a30a9455c7c8b7f068550e789e90560045e4b 100644
--- a/src/music.c
+++ b/src/music.c
@@ -26,6 +26,8 @@
 #include "music.h"
 #include "script.h"
 #include "tSDL.h"
+#include "physfs.h"
+#include "physfsrwops.h"
 
 bool sound_active = TRUE;
 
@@ -63,7 +65,7 @@ static int music_play(lua_State *L)
 	int loop = lua_isnumber(L, 2) ? lua_tonumber(L, 2) : 1;
 	int fadein = lua_isnumber(L, 3) ? lua_tonumber(L, 3) : 0;
 
-	printf("play music %x %d %d\n", *m, loop, fadein);
+	printf("play music %x %d %d\n", (unsigned int)(*m), loop, fadein);
 	lua_pushboolean(L, (Mix_FadeInMusic(*m, loop, fadein) == -1) ? FALSE : TRUE);
 	return 1;
 }
diff --git a/src/particles.c b/src/particles.c
index 71450069fa8a8836e28c972f0e193ec099a237b8..2529b7b3cea7dcb15524c3876321683c5e66479f 100644
--- a/src/particles.c
+++ b/src/particles.c
@@ -27,6 +27,7 @@
 #include "script.h"
 #include <math.h>
 #include "tSDL.h"
+#include "SFMT.h"
 
 #define rng(x, y) (x + rand_div(1 + y - x))
 
diff --git a/src/tSDL.h b/src/tSDL.h
index 6ef880bcf57336526b650dd64c961e4c9989987a..8b7b9cb426e0345c0fcb838a1fc705429f3c7f60 100644
--- a/src/tSDL.h
+++ b/src/tSDL.h
@@ -2,8 +2,10 @@
 #include <SDL/SDL.h>
 #include <SDL_ttf/SDL_ttf.h>
 #include <SDL_mixer/SDL_mixer.h>
+#include <SDL_image/SDL_image.h>
 #else
 #include <SDL.h>
 #include <SDL_ttf.h>
 #include <SDL_mixer.h>
+#include <SDL_image.h>
 #endif