diff --git a/src/template/aix b/src/template/aix
index 96641125bb49ff5f4884020d477a93ff34ffbda7..48f1110f3e8a59d9ec2f8fd8a669a81ff54130b9 100644
--- a/src/template/aix
+++ b/src/template/aix
@@ -1,10 +1,10 @@
 if test "$GCC" != yes ; then
   case $host_os in
     aix3.2.5 | aix4.1*)
-      CFLAGS="$CFLAGS -O -qmaxmem=16384 -qsrcmsg"
+      CFLAGS="-O -qmaxmem=16384 -qsrcmsg"
       ;;
     *)
-      CFLAGS="$CFLAGS -O2 -qmaxmem=16384 -qsrcmsg -qlonglong"
+      CFLAGS="-O2 -qmaxmem=16384 -qsrcmsg -qlonglong"
       ;;
   esac
 fi
diff --git a/src/template/freebsd b/src/template/freebsd
index 48fc9dfa34c5c1009e3fd4cc10add441a46fbaa0..718359e07c66d9f450745d9fb505ed4544c8f90e 100644
--- a/src/template/freebsd
+++ b/src/template/freebsd
@@ -1,3 +1,3 @@
 case $host_cpu in
-  alpha*)   CFLAGS="$CFLAGS -O";;  # alpha has problems with -O2
+  alpha*)   CFLAGS="-O";;  # alpha has problems with -O2
 esac
diff --git a/src/template/hpux b/src/template/hpux
index f8b6197f80c1e01fd314b14f1a52de9a51c9bab6..775c55a935361b4a5745b5b77d32490877683942 100644
--- a/src/template/hpux
+++ b/src/template/hpux
@@ -1,8 +1,8 @@
-CPPFLAGS="$CPPFLAGS -D_XOPEN_SOURCE_EXTENDED"
+CPPFLAGS="-D_XOPEN_SOURCE_EXTENDED"
 
 if test "$GCC" != yes ; then
   CC="$CC -Ae"
-  CFLAGS="$CFLAGS +O2"
+  CFLAGS="+O2"
 fi
 
 # Pick right test-and-set (TAS) code.  We need out-of-line assembler
diff --git a/src/template/linux b/src/template/linux
index ecb7ba4baba6e709f1c797ac7eb1c6751ecfdde5..dfb391ea54279f8476b82cf907a7f19d845fc0d6 100644
--- a/src/template/linux
+++ b/src/template/linux
@@ -1,2 +1,2 @@
 # Force _GNU_SOURCE on; plperl is broken with Perl 5.8.0 otherwise
-CPPFLAGS="$CPPFLAGS -D_GNU_SOURCE"
+CPPFLAGS="-D_GNU_SOURCE"
diff --git a/src/template/osf b/src/template/osf
index fbe4ab03ebeb64152e3459b8ed1190e7f35e402a..e845cfecea8dd668a39177a0811f4491ff8e1886 100644
--- a/src/template/osf
+++ b/src/template/osf
@@ -1,4 +1,4 @@
 if test "$GCC" != yes ; then
   CC="$CC -std"
-  CFLAGS="$CFLAGS -O -ieee"
+  CFLAGS="-O -ieee"
 fi
diff --git a/src/template/solaris b/src/template/solaris
index 93f3cd730dad25cc9de467cf9caf9cfd18c5406f..e6ee10f560bd29370abef565895c5fbace3733f0 100644
--- a/src/template/solaris
+++ b/src/template/solaris
@@ -1,6 +1,9 @@
 if test "$GCC" != yes ; then
   CC="$CC -Xa"			# relaxed ISO C mode
-  CFLAGS="$CFLAGS -O -v"		# -v is like gcc -Wall
+  CFLAGS="-v"			# -v is like gcc -Wall
+  if test "$enable_debug" != yes; then
+    CFLAGS="$CFLAGS -O"		# any optimization breaks debug
+  fi
 fi
 
 # Pick right test-and-set (TAS) code.  We need out-of-line assembler
diff --git a/src/template/univel b/src/template/univel
index 12d11496735c568166808636f37d3b6ab1500bd0..30980fee0a5981987f46d85daa90eba982a23da9 100644
--- a/src/template/univel
+++ b/src/template/univel
@@ -1,2 +1,2 @@
-CFLAGS="$CFLAGS -v -O -K i486,host,inline,loop_unroll -Dsvr4"
+CFLAGS="-v -O -K i486,host,inline,loop_unroll -Dsvr4"
 LIBS="-lc89"
diff --git a/src/template/unixware b/src/template/unixware
index a4125af2d9a3d5df1c8bba4de6dea3b5c7302f93..499eb9c3497da265654a937af97bd5bfd5cba262 100644
--- a/src/template/unixware
+++ b/src/template/unixware
@@ -13,9 +13,9 @@ void g(void){
 __EOF__
 
   if $CC -c -O -Kinline conftest.c >conftest.err 2>&1; then
-    CFLAGS="$CFLAGS -O -Kinline"
+    CFLAGS="-O -Kinline"
   else
-    CFLAGS="$CFLAGS -O -Kinline,no_host"
+    CFLAGS="-O -Kinline,no_host"
   fi
   rm -f conftest.*