--- a/pdns/Makefile.am
+++ b/pdns/Makefile.am
@@ -54,7 +54,8 @@
 	bindparser.h \
 	named.conf.parsertest \
 	delaypipe.hh delaypipe.cc \
-	pdns.service.in
+	pdns.service.in \
+	lua_hpp.mk
 
 BUILT_SOURCES = \
 	bind-dnssec.schema.sqlite3.sql.h \
@@ -109,6 +108,12 @@
 
 endif
 
+if !HAVE_LUA_HPP
+BUILT_SOURCES += lua.hpp
+nodist_pdns_server_SOURCES = lua.hpp
+CLEANFILES += lua.hpp
+endif
+
 EXTRA_PROGRAMS = \
 	calidns \
 	comfun \
@@ -1303,3 +1305,7 @@
 	pdns.service \
 	pdns@.service
 endif
+
+if !HAVE_LUA_HPP
+include lua_hpp.mk
+endif
--- /dev/null
+++ b/pdns/lua_hpp.mk
@@ -0,0 +1,6 @@
+lua.hpp:
+	$(AM_V_GEN)echo 'extern "C" {' > $@
+	@echo '#include "lua.h"' >> $@
+	@echo '#include "lualib.h"' >> $@
+	@echo '#include "lauxlib.h"' >> $@
+	@echo '}' >> $@
--- a/modules/luabackend/Makefile.am
+++ b/modules/luabackend/Makefile.am
@@ -15,5 +15,15 @@
 	slave.cc \
 	supermaster.cc
 
+if !HAVE_LUA_HPP
+BUILT_SOURCES = lua.hpp
+nodist_libluabackend_la_SOURCES = lua.hpp
+CLEANFILES = lua.hpp
+endif
+
 libluabackend_la_LDFLAGS = -module -avoid-version
 libluabackend_la_LIBADD = $(LUA_LIBS)
+
+if !HAVE_LUA_HPP
+include ../../pdns/lua_hpp.mk
+endif
