diff --git a/lib/Makefile.am b/lib/Makefile.am
index 7a52a61..bb49674 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -1,4 +1,8 @@
+if GNUCASH_ENABLE_GUI
 SUBDIRS = libc guile-www stf
+else
+SUBDIRS = libc guile-www
+endif
 
 EXTRA_DIST = README
 
diff --git a/src/Makefile.am b/src/Makefile.am
index 9fd3c21..f7e1d02 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -22,6 +22,7 @@ endif
 
 ALMOST_NONGUI_SUBDIRS = \
   app-utils \
+  optional \
   ${PYTHON_DIR}
 
 # These directories contain the code with gtk dependency
@@ -34,7 +35,6 @@ GUI_SUBDIRS = \
   gnome \
   import-export \
   business \
-  optional \
   plugins \
   bin
 
