diff --git a/build/configure b/build/configure
index 22808d5f4190243353c57e6d5fbfe52b8368b68e..5fa0692ffb1fd46bed6a5ab4ad5bd3d331f365a9 100755
--- a/build/configure
+++ b/build/configure
@@ -25366,33 +25366,40 @@ then :
     fi # end of 64-bit decision tree
     pkg-config --version > /dev/null
     use_pkg_config=$?
-    if test "x$use_pkg_config" = "x0"; then
-      # pkg-config is available
-      declare -a pkg_array=$(pkg-config --list-all | grep cudart)
-      for i in "${pkg_array[@]}"; do echo "$i" | cut --delimiter=" " -f1; done | grep cudart > /dev/null
-      result=$?
-      if test "x$result" = "x0"; then
-        # CUDA runtime detected
-        cuda_pkg=$(for i in "${pkg_array[@]}"; do echo "$i" | cut --delimiter=" " -f1; done | grep cudart)
-	CUDAFLAGS=$(pkg-config --cflags ${cuda_pkg})
-	CUDALDFLAGS=$(pkg-config --libs ${cuda_pkg})
-      fi # end of CUDA runtime decision tree
-    else
-      # pkg-config is not available
-      if test -f /usr/local/cuda/include/cuda.h; then
-        CUDAFLAGS="-I/usr/local/cuda/include"
-        CUDALDFLAGS="-L/usr/local/cuda/lib64 -lcudart"
-      elif test -f /usr/include/cuda.h; then
-        CUDAFLAGS="-I/usr/include"
-        CUDALDFLAGS="-lcudart"
-      elif test "x$CUDA_HOME" != "x"; then
-        CUDAFLAGS="-I${CUDA_HOME}/include"
-        CUDALDFLAGS="-L${CUDA_HOME}/lib64 -lcudart"
-      fi
-    fi # end of pkg-config decision tree
+    if test "x${CUDAFLAGS}${CUDALDFLAGS}" = "x"; then
+      if test "x$use_pkg_config" = "x0"; then
+        # pkg-config is available
+        declare -a pkg_array=$(pkg-config --list-all | grep cudart)
+        for i in "${pkg_array[@]}"; do echo "$i" | cut --delimiter=" " -f1; done | grep cudart > /dev/null
+        result=$?
+        if test "x$result" = "x0"; then
+          # CUDA runtime detected
+          cuda_pkg=$(for i in "${pkg_array[@]}"; do echo "$i" | cut --delimiter=" " -f1; done | grep cudart)
+    	CUDAFLAGS=$(pkg-config --cflags ${cuda_pkg})
+    	CUDALDFLAGS=$(pkg-config --libs ${cuda_pkg})
+        fi # end of CUDA runtime decision tree
+        echo $CUDALDFLAGS | grep cudart > /dev/null
+        cudart_check=$?
+        if test "x${cudart_check}" != "x0"; then
+          CUDALDFLAGS="$CUDALDFLAGS -lcudart"
+        fi
+      else
+        # pkg-config is not available
+        if test -f /usr/local/cuda/include/cuda.h; then
+          CUDAFLAGS="-I/usr/local/cuda/include"
+          CUDALDFLAGS="-L/usr/local/cuda/lib64 -lcudart"
+        elif test -f /usr/include/cuda.h; then
+          CUDAFLAGS="-I/usr/include"
+          CUDALDFLAGS="-lcudart"
+        elif test "x$CUDA_HOME" != "x"; then
+          CUDAFLAGS="-I${CUDA_HOME}/include"
+          CUDALDFLAGS="-L${CUDA_HOME}/lib64 -lcudart"
+        fi
+      fi # end of pkg-config decision tree
+    fi # end of CUDAFLAGS user override protection
     if test "x${MAGMA_ROOT}${MAGMA_HOME}${MAGMA_DIR}" = "x"; then
       # MAGMA environment is not defined
-      if "x$use_pkg_config" = "x0"; then
+      if test "x$use_pkg_config" = "x0"; then
         # use pkg-config to search for MAGMA
         declare -a pkg_array=$(pkg-config --list-all | grep magma)
         for i in "${pkg_array[@]}"; do echo "$i" | cut --delimiter=" " -f1; done | grep magma > /dev/null
@@ -25401,7 +25408,7 @@ then :
 	  # MAGMA was found
 	  magma_pkg=$(for i in "${pkg_array[@]}"; do echo "$i" | cut --delimiter=" " -f1; done | grep magma)
 	  MAGMA_INCLUDE=$(pkg-config --cflags-only-I ${magma_pkg})
-	  MAGMA_LIBS_DIR=$(pkg-config --cflags-only-L ${magma_pkg})
+	  MAGMA_LIBS_DIR=$(pkg-config --libs-only-L ${magma_pkg})
           export MAGMAFLAGS="-DUSE_MAGMA ${MAGMA_ILP64_FLAG} $CUDAFLAGS ${MAGMA_INCLUDE}"
           export MAGMALDFLAGS="$CUDALDFLAGS ${MAGMA_LIBS_DIR} -lmagma"
 	fi # end of MAGMA decision tree
@@ -25459,33 +25466,40 @@ else case e in #(
     fi # end of 64-bit decision tree
     pkg-config --version > /dev/null
     use_pkg_config=$?
-    if test "x$use_pkg_config" = "x0"; then
-      # pkg-config is available
-      declare -a pkg_array=$(pkg-config --list-all | grep cudart)
-      for i in "${pkg_array[@]}"; do echo "$i" | cut --delimiter=" " -f1; done | grep cudart > /dev/null
-      result=$?
-      if test "x$result" = "x0"; then
-        # CUDA runtime detected
-        cuda_pkg=$(for i in "${pkg_array[@]}"; do echo "$i" | cut --delimiter=" " -f1; done | grep cudart)
-	CUDAFLAGS=$(pkg-config --cflags ${cuda_pkg})
-	CUDALDFLAGS=$(pkg-config --libs ${cuda_pkg})
-      fi # end of CUDA runtime decision tree
-    else
-      # pkg-config is not available
-      if test -f /usr/local/cuda/include/cuda.h; then
-        CUDAFLAGS="-I/usr/local/cuda/include"
-        CUDALDFLAGS="-L/usr/local/cuda/lib64 -lcudart"
-      elif test -f /usr/include/cuda.h; then
-        CUDAFLAGS="-I/usr/include"
-        CUDALDFLAGS="-lcudart"
-      elif test "x$CUDA_HOME" != "x"; then
-        CUDAFLAGS="-I${CUDA_HOME}/include"
-        CUDALDFLAGS="-L${CUDA_HOME}/lib64 -lcudart"
-      fi
-    fi # end of pkg-config decision tree
+    if test "x${CUDAFLAGS}${CUDALDFLAGS}" = "x"; then
+      if test "x$use_pkg_config" = "x0"; then
+        # pkg-config is available
+        declare -a pkg_array=$(pkg-config --list-all | grep cudart)
+        for i in "${pkg_array[@]}"; do echo "$i" | cut --delimiter=" " -f1; done | grep cudart > /dev/null
+        result=$?
+        if test "x$result" = "x0"; then
+          # CUDA runtime detected
+          cuda_pkg=$(for i in "${pkg_array[@]}"; do echo "$i" | cut --delimiter=" " -f1; done | grep cudart)
+    	CUDAFLAGS=$(pkg-config --cflags ${cuda_pkg})
+    	CUDALDFLAGS=$(pkg-config --libs ${cuda_pkg})
+        fi # end of CUDA runtime decision tree
+        echo $CUDALDFLAGS | grep cudart > /dev/null
+        cudart_check=$?
+        if test "x${cudart_check}" != "x0"; then
+          CUDALDFLAGS="$CUDALDFLAGS -lcudart"
+        fi
+      else
+        # pkg-config is not available
+        if test -f /usr/local/cuda/include/cuda.h; then
+          CUDAFLAGS="-I/usr/local/cuda/include"
+          CUDALDFLAGS="-L/usr/local/cuda/lib64 -lcudart"
+        elif test -f /usr/include/cuda.h; then
+          CUDAFLAGS="-I/usr/include"
+          CUDALDFLAGS="-lcudart"
+        elif test "x$CUDA_HOME" != "x"; then
+          CUDAFLAGS="-I${CUDA_HOME}/include"
+          CUDALDFLAGS="-L${CUDA_HOME}/lib64 -lcudart"
+        fi
+      fi # end of pkg-config decision tree
+    fi # end of CUDAFLAGS user override protection
     if test "x${MAGMA_ROOT}${MAGMA_HOME}${MAGMA_DIR}" = "x"; then
       # MAGMA environment is not defined
-      if "x$use_pkg_config" = "x0"; then
+      if test "x$use_pkg_config" = "x0"; then
         # use pkg-config to search for MAGMA
         declare -a pkg_array=$(pkg-config --list-all | grep magma)
         for i in "${pkg_array[@]}"; do echo "$i" | cut --delimiter=" " -f1; done | grep magma > /dev/null
@@ -25494,7 +25508,7 @@ else case e in #(
 	  # MAGMA was found
 	  magma_pkg=$(for i in "${pkg_array[@]}"; do echo "$i" | cut --delimiter=" " -f1; done | grep magma)
 	  MAGMA_INCLUDE=$(pkg-config --cflags-only-I ${magma_pkg})
-	  MAGMA_LIBS_DIR=$(pkg-config --cflags-only-L ${magma_pkg})
+	  MAGMA_LIBS_DIR=$(pkg-config --libs-only-L ${magma_pkg})
           export MAGMAFLAGS="-DUSE_MAGMA ${MAGMA_ILP64_FLAG} $CUDAFLAGS ${MAGMA_INCLUDE}"
           export MAGMALDFLAGS="$CUDALDFLAGS ${MAGMA_LIBS_DIR} -lmagma"
 	fi # end of MAGMA decision tree
diff --git a/build/configure.ac b/build/configure.ac
index 5b30c9aa30f1fd1caf4cd65d69edacd775a41012..7ff1a35c695fef8c5c07baefcd69ee239d4e48eb 100644
--- a/build/configure.ac
+++ b/build/configure.ac
@@ -160,33 +160,40 @@ m4_define(
     fi # end of 64-bit decision tree
     pkg-config --version > /dev/null
     use_pkg_config=$?
-    if test "x$use_pkg_config" = "x0"; then
-      # pkg-config is available
-      declare -a pkg_array=$(pkg-config --list-all | grep cudart)
-      for i in "${pkg_array[[@]]}"; do echo "$i" | cut --delimiter=" " -f1; done | grep cudart > /dev/null
-      result=$?
-      if test "x$result" = "x0"; then
-        # CUDA runtime detected
-        cuda_pkg=$(for i in "${pkg_array[[@]]}"; do echo "$i" | cut --delimiter=" " -f1; done | grep cudart)
-	CUDAFLAGS=$(pkg-config --cflags ${cuda_pkg})
-	CUDALDFLAGS=$(pkg-config --libs ${cuda_pkg})
-      fi # end of CUDA runtime decision tree
-    else
-      # pkg-config is not available
-      if test -f /usr/local/cuda/include/cuda.h; then
-        CUDAFLAGS="-I/usr/local/cuda/include"
-        CUDALDFLAGS="-L/usr/local/cuda/lib64 -lcudart"
-      elif test -f /usr/include/cuda.h; then
-        CUDAFLAGS="-I/usr/include"
-        CUDALDFLAGS="-lcudart"
-      elif test "x$CUDA_HOME" != "x"; then
-        CUDAFLAGS="-I${CUDA_HOME}/include"
-        CUDALDFLAGS="-L${CUDA_HOME}/lib64 -lcudart"
-      fi
-    fi # end of pkg-config decision tree
+    if test "x${CUDAFLAGS}${CUDALDFLAGS}" = "x"; then
+      if test "x$use_pkg_config" = "x0"; then
+        # pkg-config is available
+        declare -a pkg_array=$(pkg-config --list-all | grep cudart)
+        for i in "${pkg_array[[@]]}"; do echo "$i" | cut --delimiter=" " -f1; done | grep cudart > /dev/null
+        result=$?
+        if test "x$result" = "x0"; then
+          # CUDA runtime detected
+          cuda_pkg=$(for i in "${pkg_array[[@]]}"; do echo "$i" | cut --delimiter=" " -f1; done | grep cudart)
+    	CUDAFLAGS=$(pkg-config --cflags ${cuda_pkg})
+    	CUDALDFLAGS=$(pkg-config --libs ${cuda_pkg})
+        fi # end of CUDA runtime decision tree
+        echo $CUDALDFLAGS | grep cudart > /dev/null
+        cudart_check=$?
+        if test "x${cudart_check}" != "x0"; then
+          CUDALDFLAGS="$CUDALDFLAGS -lcudart"
+        fi
+      else
+        # pkg-config is not available
+        if test -f /usr/local/cuda/include/cuda.h; then
+          CUDAFLAGS="-I/usr/local/cuda/include"
+          CUDALDFLAGS="-L/usr/local/cuda/lib64 -lcudart"
+        elif test -f /usr/include/cuda.h; then
+          CUDAFLAGS="-I/usr/include"
+          CUDALDFLAGS="-lcudart"
+        elif test "x$CUDA_HOME" != "x"; then
+          CUDAFLAGS="-I${CUDA_HOME}/include"
+          CUDALDFLAGS="-L${CUDA_HOME}/lib64 -lcudart"
+        fi
+      fi # end of pkg-config decision tree
+    fi # end of CUDAFLAGS user override protection
     if test "x${MAGMA_ROOT}${MAGMA_HOME}${MAGMA_DIR}" = "x"; then
       # MAGMA environment is not defined
-      if "x$use_pkg_config" = "x0"; then
+      if test "x$use_pkg_config" = "x0"; then
         # use pkg-config to search for MAGMA
         declare -a pkg_array=$(pkg-config --list-all | grep magma)
         for i in "${pkg_array[[@]]}"; do echo "$i" | cut --delimiter=" " -f1; done | grep magma > /dev/null
@@ -195,7 +202,7 @@ m4_define(
 	  # MAGMA was found
 	  magma_pkg=$(for i in "${pkg_array[[@]]}"; do echo "$i" | cut --delimiter=" " -f1; done | grep magma)
 	  MAGMA_INCLUDE=$(pkg-config --cflags-only-I ${magma_pkg})
-	  MAGMA_LIBS_DIR=$(pkg-config --cflags-only-L ${magma_pkg})
+	  MAGMA_LIBS_DIR=$(pkg-config --libs-only-L ${magma_pkg})
           export MAGMAFLAGS="-DUSE_MAGMA ${MAGMA_ILP64_FLAG} $CUDAFLAGS ${MAGMA_INCLUDE}"
           export MAGMALDFLAGS="$CUDALDFLAGS ${MAGMA_LIBS_DIR} -lmagma"
 	fi # end of MAGMA decision tree