diff --git a/ChangeLog b/ChangeLog
index a213a3fa4f8e98ba09eb277f956c6a29586ce9e9..5ef350e35984c43f4e4b882ea5ebe37624641b0a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2014-09-03  Niels Möller  <nisse@lysator.liu.se>
+
+	* aclocal.m4 (LSH_CCPIC): Don't substitute CCPIC here, let
+	configure.ac do that if needed.
+
+	* configure.ac (CCPIC_MAYBE, SHLIBCFLAGS): Deleted substituted
+	variables. Instead, use CCPIC directly when compiling all library
+	files.
+	(CCPIC): Set to empty, if --disable-pic is used.
+
+	* config.make.in (SHLIBCFLAGS, CCPIC_MAYBE): Deleted.
+	(COMPILE, COMPILE_CXX): Drop CCPIC. New variable EXTRA_CFLAGS,
+	which can be set by individual Makefiles.
+
+	* Makefile.in (EXTRA_CFLAGS): Set using CCPIC.
+	Also delete all uses of CCPIC_MAYBE and SHLIBCFLAGS.
+
 2014-09-02  Niels Möller  <nisse@lysator.liu.se>
 
 	* curve25519-eh-to-x.c (curve25519_eh_to_x): New file, new
diff --git a/Makefile.in b/Makefile.in
index 50fcfb4b7b6dcd842e28639119ca6cf3971a46d9..3f859b2a0ea221b2917bce99eacf63db07bc801f 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -23,6 +23,7 @@ include config.make
 
 # $(srcdir) is needed for includes in bignum.h.
 PRE_CPPFLAGS = -I. -I$(srcdir)
+EXTRA_CFLAGS = $(CCPIC)
 
 # FIXME: Add configuration of LIBEXT?
 LIBTARGETS = @IF_STATIC@ libnettle.a @IF_HOGWEED@ libhogweed.a
@@ -238,7 +239,7 @@ libhogweed.a: $(hogweed_OBJS)
 	echo hogweed > libhogweed.stamp
 
 .c.$(OBJEXT):
-	$(COMPILE) $(CCPIC_MAYBE) -c $< \
+	$(COMPILE) -c $< \
 	&& $(DEP_PROCESS)
 
 # Rules building shared libraries.
@@ -265,7 +266,7 @@ $(LIBHOGWEED_FORLINK): $(hogweed_PURE_OBJS) $(LIBNETTLE_FORLINK)
 	echo hogweed > libhogweed.stamp
 
 .c.p$(OBJEXT):
-	$(COMPILE) $(SHLIBCFLAGS) -c $< -o $@ \
+	$(COMPILE) -c $< -o $@ \
 	&& $(DEP_PROCESS)
 
 # For Solaris and BSD make, we have to use an explicit rule for each
@@ -378,11 +379,11 @@ ecc-25519.p$(OBJEXT): ecc-25519.h
 	&& test -s $@T && mv -f $@T $@
 
 .s.$(OBJEXT):
-	$(COMPILE) $(CCPIC_MAYBE) -c $<
+	$(COMPILE) -c $<
 	@echo > $@.d
 
 .s.p$(OBJEXT):
-	$(COMPILE) $(SHLIBCFLAGS) -c $< -o $@
+	$(COMPILE) -c $< -o $@
 	@echo > $@.d
 
 # Texinfo rules
diff --git a/aclocal.m4 b/aclocal.m4
index b77b88633b7ae6c4dbbd6972b199ba751c9b6824..36daec6fce1988884a1740a2663c1d8b91fff352 100644
--- a/aclocal.m4
+++ b/aclocal.m4
@@ -58,8 +58,7 @@ AC_CACHE_VAL(lsh_cv_sys_ccpic,[
   CFLAGS="$OLD_CFLAGS"
 ])
 CCPIC="$lsh_cv_sys_ccpic"
-AC_MSG_RESULT($CCPIC)
-AC_SUBST([CCPIC])])
+AC_MSG_RESULT($CCPIC)])
 
 dnl LSH_PATH_ADD(path-id, directory)
 AC_DEFUN([LSH_PATH_ADD],
diff --git a/config.make.in b/config.make.in
index ac3393dee1c63cd4e405f627ec60c70ccbead937..a8ab49faa714ca3cd74cf4962c3ba7e1c518827f 100644
--- a/config.make.in
+++ b/config.make.in
@@ -5,7 +5,6 @@ CXX = @CXX@
 CFLAGS = @CFLAGS@
 CXXFLAGS = @CXXFLAGS@
 CCPIC = @CCPIC@
-CCPIC_MAYBE = @CCPIC_MAYBE@
 CPPFLAGS = @CPPFLAGS@
 DEFS = @DEFS@
 LDFLAGS = @LDFLAGS@
@@ -29,8 +28,6 @@ PACKAGE_STRING = @PACKAGE_STRING@
 PACKAGE_TARNAME = @PACKAGE_TARNAME@
 PACKAGE_VERSION = @PACKAGE_VERSION@
 
-SHLIBCFLAGS = @SHLIBCFLAGS@
-
 LIBNETTLE_MAJOR = @LIBNETTLE_MAJOR@
 LIBNETTLE_MINOR = @LIBNETTLE_MINOR@
 LIBNETTLE_SONAME = @LIBNETTLE_SONAME@
@@ -69,10 +66,10 @@ includedir =	@includedir@
 infodir =	@infodir@
 
 # PRE_CPPFLAGS and PRE_LDFLAGS lets each Makefile.in prepend its own
-# flags before CPPFLAGS and LDFLAGS.
+# flags before CPPFLAGS and LDFLAGS. While EXTRA_CFLAGS are added at the end.
 
-COMPILE = $(CC) $(PRE_CPPFLAGS) $(CPPFLAGS) $(DEFS) $(CFLAGS) $(CCPIC) $(DEP_FLAGS)
-COMPILE_CXX = $(CXX) $(PRE_CPPFLAGS) $(CPPFLAGS) $(DEFS) $(CXXFLAGS) $(CCPIC) $(DEP_FLAGS)
+COMPILE = $(CC) $(PRE_CPPFLAGS) $(CPPFLAGS) $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) $(DEP_FLAGS)
+COMPILE_CXX = $(CXX) $(PRE_CPPFLAGS) $(CPPFLAGS) $(DEFS) $(CXXFLAGS) $(DEP_FLAGS)
 LINK = $(CC) $(CFLAGS) $(PRE_LDFLAGS) $(LDFLAGS)
 LINK_CXX = $(CXX) $(CXXFLAGS) $(PRE_LDFLAGS) $(LDFLAGS)
 
diff --git a/configure.ac b/configure.ac
index 19fe06cb47539312106fc236be96c853dc06406f..4591701d62e3325db1dd9945c79160ffc1453e6e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -390,9 +390,12 @@ AC_CONFIG_COMMANDS([asm.d],
     [ asm_file_list="$asm_file_list" ]
 )
 
-LSH_CCPIC
-
-SHLIBCFLAGS="$CCPIC"
+if test "x$enable_pic" = xyes; then
+    LSH_CCPIC
+else
+    CCPIC=''
+fi
+AC_SUBST(CCPIC)
 
 IF_DLL='#'
 LIBNETTLE_FILE_SRC='$(LIBNETTLE_FORLINK)'
@@ -490,13 +493,6 @@ case "$host_os" in
     ;;
 esac
 
-if test "x$enable_pic" = xyes; then
-  CCPIC_MAYBE="$CCPIC"
-else
-  CCPIC_MAYBE=''
-fi
-AC_SUBST([CCPIC_MAYBE])
-
 ASM_SYMBOL_PREFIX=''
 ASM_ELF_STYLE='no'
 ASM_COFF_STYLE='no'
@@ -627,8 +623,6 @@ AC_SUBST(ASM_ALIGN_LOG)
 AC_SUBST(W64_ABI)
 AC_SUBST(EMULATOR)
 
-AC_SUBST(SHLIBCFLAGS)
-
 AC_SUBST(LIBNETTLE_MAJOR)
 AC_SUBST(LIBNETTLE_MINOR)
 AC_SUBST(LIBNETTLE_FORLINK)