--- a/configure.ac
+++ b/configure.ac
@@ -1,4 +1,12 @@
 AC_INIT([rjags], [2.1.0])
+
+if test -z "${R_HOME}"; then
+   AC_MSG_ERROR("R_HOME is not defined")
+fi
+CC=`"${R_HOME}/bin/R" CMD config CC`
+CXX=`"${R_HOME}/bin/R" CMD config CXX`
+CXXFLAGS=`"${R_HOME}/bin/R" CMD config CXXFLAGS`
+CPPFLAGS=`"${R_HOME}/bin/R" CMD config CPPFLAGS`
 
 AC_PREFIX_PROGRAM(jags)
 
@@ -41,10 +49,7 @@
 # override this.
 
 if test -z "${LIBnn}"; then 
-   if test -z "${R_HOME}"; then
-      AC_MSG_ERROR("R_HOME is not defined")
-   fi
-   LIBnn=`${R_HOME}/bin/R CMD config LIBnn`
+   LIBnn=`"${R_HOME}/bin/R" CMD config LIBnn`
 fi
 
 #Set optional library path with option --with-jags-lib or