diff --git a/src/msvcbuild.bat b/src/msvcbuild.bat
index bd0c4c5..95e1cf4 100644
--- a/src/msvcbuild.bat
+++ b/src/msvcbuild.bat
@@ -67,15 +67,20 @@ buildvm -m folddef -o lj_folddef.h lj_opt_fold.c
 @shift
 @set LJCOMPILE=%LJCOMPILE% /Zi
 @set LJLINK=%LJLINK% /debug
+@set DEBUG=1
 :NODEBUG
 @if "%1"=="amalg" goto :AMALGDLL
 @if "%1"=="static" goto :STATIC
-%LJCOMPILE% /MD /DLUA_BUILD_AS_DLL lj_*.c lib_*.c
+@if "%DEBUG%"=="1" set LJCOMPILE=%LJCOMPILE% /MDd
+@if "%DEBUG%"=="" set LJCOMPILE=%LJCOMPILE% /MD
+%LJCOMPILE% /DLUA_BUILD_AS_DLL lj_*.c lib_*.c
 @if errorlevel 1 goto :BAD
 %LJLINK% /DLL /out:%LJDLLNAME% lj_*.obj lib_*.obj
 @if errorlevel 1 goto :BAD
 @goto :MTDLL
 :STATIC
+@if "%DEBUG%"=="1" set LJCOMPILE=%LJCOMPILE% /MTd
+@if "%DEBUG%"=="" set LJCOMPILE=%LJCOMPILE% /MT
 %LJCOMPILE% lj_*.c lib_*.c
 @if errorlevel 1 goto :BAD
 %LJLIB% /OUT:%LJLIBNAME% lj_*.obj lib_*.obj
