diff --git a/src/configure b/src/configure
index 79aea1f64f112348f8a3a22556b77613255dff3d..31241ee5e81c290dceb46d9b7e68fa0d9345495a 100755
--- a/src/configure
+++ b/src/configure
@@ -5562,24 +5562,32 @@ fi
 
 if test "$USE_TCL" = "true"
 then
+ice_save_LIBS="$LIBS"
+ice_save_CFLAGS="$CFLAGS"
+ice_save_CPPFLAGS="$CPPFLAGS"
+ice_save_LDFLAGS="$LDFLAGS"
+LIBS="$LIBS $X_EXTRA_LIBS"
+CFLAGS="$CFLAGS $X_CFLAGS"
+CPPFLAGS="$CPPFLAGS $X_CFLAGS"
+LDFLAGS="$LDFLAGS $X_LIBS"
 TK_LIB=
 echo $ac_n "checking for main in -ltk""... $ac_c" 1>&6
-echo "configure:5568: checking for main in -ltk" >&5
+echo "configure:5576: checking for main in -ltk" >&5
 ac_lib_var=`echo tk'_'main | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   ac_save_LIBS="$LIBS"
-LIBS="-ltk  $LIBS"
+LIBS="-ltk $X11_LIBS $TCL_LIB $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 5576 "configure"
+#line 5584 "configure"
 #include "confdefs.h"
 
 int main() {
 main()
 ; return 0; }
 EOF
-if { (eval echo configure:5583: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
+if { (eval echo configure:5591: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -5606,6 +5614,10 @@ else
 TK_LIB=-l$TK_LIB
 fi
 
+LIBS="$ice_save_LIBS"
+CFLAGS="$ice_save_CFLAGS"
+CPPFLAGS="$ice_save_CPPFLAGS"
+LDFLAGS="$ice_save_LDFLAGS"
 fi
 
 trap '' 1 2 15
diff --git a/src/configure.in b/src/configure.in
index 9b717f1a372e15458ac5958a16b9a45dc61bb5ac..2ed02dafbf51b8907a48c5468eb0bf874009fbc1 100644
--- a/src/configure.in
+++ b/src/configure.in
@@ -620,8 +620,16 @@ fi
 dnl Check for Tk archive
 if test "$USE_TCL" = "true"
 then
+ice_save_LIBS="$LIBS"
+ice_save_CFLAGS="$CFLAGS"
+ice_save_CPPFLAGS="$CPPFLAGS"
+ice_save_LDFLAGS="$LDFLAGS"
+LIBS="$LIBS $X_EXTRA_LIBS"
+CFLAGS="$CFLAGS $X_CFLAGS"
+CPPFLAGS="$CPPFLAGS $X_CFLAGS"
+LDFLAGS="$LDFLAGS $X_LIBS"
 TK_LIB=
-AC_CHECK_LIB(tk, main, TK_LIB=tk)
+AC_CHECK_LIB(tk, main, TK_LIB=tk,,$X11_LIBS $TCL_LIB)
 if test -z "$TK_LIB"; then
 AC_MSG_WARN(tcl support disabled; Tk library missing)
 USE_TCL=
@@ -629,6 +637,10 @@ else
 TK_LIB=-l$TK_LIB
 fi
 AC_SUBST(TK_LIB)
+LIBS="$ice_save_LIBS"
+CFLAGS="$ice_save_CFLAGS"
+CPPFLAGS="$ice_save_CPPFLAGS"
+LDFLAGS="$ice_save_LDFLAGS"
 fi
 
 AC_OUTPUT(GNUmakefile Makefile.global backend/port/Makefile bin/pg_version/Makefile bin/psql/Makefile bin/pg_dump/Makefile backend/utils/Gen_fmgrtab.sh interfaces/libpq/Makefile interfaces/libpgtcl/Makefile interfaces/ecpg/lib/Makefile )