--- acinclude.m4
+++ acinclude.m4
@@ -539,6 +539,17 @@
 AC_DEFUN([CEGUI_CHECK_LUA],[
     PKG_CHECK_MODULES(Lua, lua >= 5.0 lua < 5.1, [cegui_found_lua=yes], [cegui_found_lua=no])
 
+    LUA_VERSION_FLAGS="";
+    if test x$cegui_found_lua = xno; then
+       PKG_CHECK_MODULES(Lua, lua >= 5.1, [cegui_found_lua=yes], [cegui_found_lua=no])
+       
+       if test x$cegui_found_lua = xyes; then
+	  LUA_VERSION_FLAGS="$LUA_VERSION_FLAGS -DLUA51"
+  	  AC_MSG_NOTICE([Lua version >=5.1])
+       fi
+    fi
+    AC_SUBST(LUA_VERSION_FLAGS)
+
     AC_ARG_ENABLE([lua-module], AC_HELP_STRING([--disable-lua-module], [Disables building of the Lua scripting module.]),
                 [cegui_with_lua=$enableval], [cegui_with_lua=yes])
     AC_ARG_ENABLE([toluacegui], AC_HELP_STRING([--enable-toluacegui], [Enables building of our custom tolua++ based binding generator tool, tolua++cegui.]),
--- ScriptingModules/CEGUILua/LuaScriptModule/src/CEGUILua.cpp
+++ ScriptingModules/CEGUILua/LuaScriptModule/src/CEGUILua.cpp
@@ -35,12 +35,10 @@
 
 // include Lua libs and tolua++
 extern "C" {
-#include "lua.h"
+#include "tolua++.h"
 #include "lualib.h"
-#include "lauxlib.h"
 }
 
-#include "tolua++.h"
 
 // prototype for bindings initialisation function
 int tolua_CEGUI_open(lua_State* tolua_S);
@@ -55,11 +53,36 @@
 *************************************************************************/
 LuaScriptModule::LuaScriptModule()
 {
+#ifdef LUA51
+       static const luaL_Reg lualibs[] = { 
+          {"", luaopen_base}, 
+          {LUA_LOADLIBNAME, luaopen_package}, 
+          {LUA_TABLIBNAME, luaopen_table}, 
+          {LUA_IOLIBNAME, luaopen_io}, 
+          {LUA_OSLIBNAME, luaopen_os}, 
+          {LUA_STRLIBNAME, luaopen_string}, 
+          {LUA_MATHLIBNAME, luaopen_math}, 
+#if defined(DEBUG) || defined (_DEBUG) 
+          {LUA_DBLIBNAME, luaopen_debug}, 
+#endif 
+          {NULL, NULL} 
+        };
+#endif /* LUA51 */
+
 	// create a lua state
 	d_ownsState = true;
 	d_state = lua_open();
 
 	// init all standard libraries
+#ifdef LUA51
+        const luaL_Reg *lib = lualibs; 
+        for (; lib->func; lib++) 
+        { 
+           lua_pushcfunction(d_state, lib->func); 
+           lua_pushstring(d_state, lib->name); 
+           lua_call(d_state, 1, 0); 
+        }	
+#else /* LUA51 */
 	luaopen_base(d_state);
 	luaopen_io(d_state);
 	luaopen_string(d_state);
@@ -68,7 +91,7 @@
 #if defined(DEBUG) || defined (_DEBUG)
 	luaopen_debug(d_state);
 #endif
-
+#endif /* LUA51 */
 	setModuleIdentifierString();
 }
 
--- ScriptingModules/CEGUILua/LuaScriptModule/src/Makefile.am
+++ ScriptingModules/CEGUILua/LuaScriptModule/src/Makefile.am
@@ -1,6 +1,7 @@
 INCLUDES = -I$(top_srcdir)/include \
            -I$(top_srcdir)/ScriptingModules/CEGUILua/tolua++ \
-           -I$(top_srcdir)/ScriptingModules/CEGUILua/LuaScriptModule/include
+           -I$(top_srcdir)/ScriptingModules/CEGUILua/LuaScriptModule/include \
+           ${LUA_VERSION_FLAGS}
 lib_LTLIBRARIES = libCEGUILuaScriptModule.la
 
 libCEGUILuaScriptModule_la_LDFLAGS = -version-info @CEGUI_VERSION_INFO@
