diff --git a/configure.ac b/configure.ac
index ea5acd825193e52458a9b6efcd16bdbe292026ac..e6d104ef5208ce409e502505820d3337a005c238 100644
--- a/configure.ac
+++ b/configure.ac
@@ -298,27 +298,33 @@ AC_ARG_WITH([tcmalloc],
 )
 if test "x$with_tcmalloc" != "xno"; then
    if test "x$with_tcmalloc" != "xyes" && test "x$with_tcmalloc" != "x"; then
-      TCMALLOC_LIBS="-L$with_tcmalloc -ltcmalloc"
+      tclibs="-L$with_tcmalloc -ltcmalloc"
    else
-      TCMALLOC_LIBS="-ltcmalloc"
+      tclibs="-ltcmalloc"
    fi
    AC_CHECK_LIB([tcmalloc],[tc_cfree],[have_tcmalloc="yes"],[have_tcmalloc="no"],
-                $TCMALLOC_LIBS)
+                $tclibs)
 
    #  Could just have the minimal version.
    if test "$have_tcmalloc" = "no"; then
       if test "x$with_tcmalloc" != "xyes" && test "x$with_tcmalloc" != "x"; then
-         TCMALLOC_LIBS="-L$with_tcmalloc -ltcmalloc_minimal"
+         tclibs="-L$with_tcmalloc -ltcmalloc_minimal"
       else
-         TCMALLOC_LIBS="-ltcmalloc_minimal"
+         tclibs="-ltcmalloc_minimal"
       fi
       AC_CHECK_LIB([tcmalloc],[tc_cfree],[have_tcmalloc="yes"],[have_tcmalloc="no"],
-                   $TCMALLOC_LIBS)
+                   $tclibs)
    fi
 
-   # These are recommended for GCC.
-   if test "$have_tcmalloc" = "yes" -a "$ax_cv_c_compiler_vendor" = "gnu"; then
-      CFLAGS="$CFLAGS -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free"
+   if test "$have_tcmalloc" = "yes"; then
+      TCMALLOC_LIBS="$tclibs"
+
+      # These are recommended for GCC.
+      if "$ax_cv_c_compiler_vendor" = "gnu"; then
+         CFLAGS="$CFLAGS -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free"
+      fi
+   else
+      TCMALLOC_LIBS=""
    fi
 fi
 AC_SUBST([TCMALLOC_LIBS])
@@ -335,12 +341,18 @@ AC_ARG_WITH([profiler],
 )
 if test "x$with_profiler" != "xno"; then
    if test "x$with_profiler" != "xyes" && test "x$with_profiler" != "x"; then
-      PROFILER_LIBS="-L$with_profiler -lprofiler"
+      proflibs="-L$with_profiler -lprofiler"
    else
-      PROFILER_LIBS="-lprofiler"
+      proflibs="-lprofiler"
    fi
    AC_CHECK_LIB([profiler],[ProfilerFlush],[have_profiler="yes"],[have_profiler="no"],
-                $PROFILER_LIBS)
+                $proflibs)
+
+   if test "$have_profiler" = "yes"; then
+      PROFILER_LIBS="$proflibs"
+   else
+      PROFILER_LIBS=""
+   fi
 fi
 AC_SUBST([PROFILER_LIBS])
 AM_CONDITIONAL([HAVEPROFILER],[test -n "$PROFILER_LIBS"])
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 7d862377a769b715598d81220caec8183781ae7e..22911e903e8bbf8265d712185f41de2bf4f53553 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -21,16 +21,17 @@ MYFLAGS = -DTIMER
 # Add the source directory and debug to CFLAGS
 AM_CFLAGS = -I../src $(HDF5_CPPFLAGS)
 
-AM_LDFLAGS =
+AM_LDFLAGS = $(HDF5_LDFLAGS)
 
-EXTRA_LIBS = @PROFILER_LIBS@ @TCMALLOC_LIBS@
+# Extra libraries.
+EXTRA_LIBS = $(HDF5_LIBS) $(PROFILER_LIBS) $(TCMALLOC_LIBS)
 
-MPI_THREAD_LIBS = @MPI_THREAD_LIBS@
+# MPI libraries.
 MPI_LIBS = $(METIS_LIBS) $(MPI_THREAD_LIBS)
 MPI_FLAGS = -DWITH_MPI $(METIS_INCS)
 
 
-# Set-up the library
+# Programs.
 bin_PROGRAMS = swift swift_fixdt
 
 # Build MPI versions as well?
@@ -48,20 +49,20 @@ endif
 # Sources for swift
 swift_SOURCES = main.c
 swift_CFLAGS = $(MYFLAGS) $(AM_CFLAGS) -DENGINE_POLICY="engine_policy_keep $(ENGINE_POLICY_SETAFFINITY)"
-swift_LDADD =  ../src/.libs/libswiftsim.a $(HDF5_LDFLAGS) $(HDF5_LIBS) $(EXTRA_LIBS)
+swift_LDADD =  ../src/.libs/libswiftsim.a $(EXTRA_LIBS)
 
 swift_fixdt_SOURCES = main.c
 swift_fixdt_CFLAGS = $(MYFLAGS) $(AM_CFLAGS) -DENGINE_POLICY="engine_policy_fixdt | engine_policy_keep $(ENGINE_POLICY_SETAFFINITY)"
-swift_fixdt_LDADD =  ../src/.libs/libswiftsim.a $(HDF5_LDFLAGS) $(HDF5_LIBS) $(EXTRA_LIBS)
+swift_fixdt_LDADD =  ../src/.libs/libswiftsim.a $(EXTRA_LIBS)
 
 # Sources for swift_mpi, do we need an affinity policy for MPI?
 swift_mpi_SOURCES = main.c
 swift_mpi_CFLAGS = $(MYFLAGS) $(AM_CFLAGS) $(MPI_FLAGS) -DENGINE_POLICY="engine_policy_keep $(ENGINE_POLICY_SETAFFINITY)"
-swift_mpi_LDADD =  ../src/.libs/libswiftsim_mpi.a $(HDF5_LDFLAGS) $(HDF5_LIBS) $(MPI_LIBS) $(EXTRA_LIBS)
+swift_mpi_LDADD =  ../src/.libs/libswiftsim_mpi.a $(MPI_LIBS) $(EXTRA_LIBS)
 
 swift_fixdt_mpi_SOURCES = main.c
 swift_fixdt_mpi_CFLAGS = $(MYFLAGS) $(AM_CFLAGS) $(MPI_FLAGS) -DENGINE_POLICY="engine_policy_fixdt | engine_policy_keep $(ENGINE_POLICY_SETAFFINITY)"
-swift_fixdt_mpi_LDADD =  ../src/.libs/libswiftsim_mpi.a $(HDF5_LDFLAGS) $(HDF5_LIBS) $(MPI_LIBS) $(EXTRA_LIBS)
+swift_fixdt_mpi_LDADD =  ../src/.libs/libswiftsim_mpi.a $(MPI_LIBS) $(EXTRA_LIBS)
 
 # Scripts to generate ICs
 EXTRA_DIST = UniformBox/makeIC.py UniformBox/run.sh UniformBox/uniformBox.yml \
diff --git a/src/Makefile.am b/src/Makefile.am
index 21b5ef25ca21202caaa9107bfbf09e62aa66011d..bc263d6fb0b2a8378e26a3627cf26204c7b09bb4 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -24,6 +24,13 @@ AM_LDFLAGS = $(HDF5_LDFLAGS) -version-info 0:0:0
 # The git command, if available.
 GIT_CMD = @GIT_CMD@
 
+# Additional dependencies for shared libraries.
+EXTRA_LIBS = $(HDF5_LIBS) $(PROFILER_LIBS) $(TCMALLOC_LIBS)
+
+# MPI libraries.
+MPI_LIBS = $(METIS_LIBS) $(MPI_THREAD_LIBS)
+MPI_FLAGS = -DWITH_MPI $(METIS_INCS)
+
 # Build the libswiftsim library
 lib_LTLIBRARIES = libswiftsim.la
 # Build a MPI-enabled version too?
@@ -65,11 +72,13 @@ nobase_noinst_HEADERS = approx_math.h atomic.h cycle.h error.h inline.h kernel_h
 
 # Sources and flags for regular library
 libswiftsim_la_SOURCES = $(AM_SOURCES)
+libswiftsim_la_CFLAGS = $(AM_CFLAGS)
+libswiftsim_la_LDFLAGS = $(AM_LDFLAGS) $(EXTRA_LIBS)
 
 # Sources and flags for MPI library
 libswiftsim_mpi_la_SOURCES = $(AM_SOURCES)
-libswiftsim_mpi_la_CFLAGS = $(AM_CFLAGS) -DWITH_MPI $(METIS_INCS)
-libswiftsim_mpi_la_LDFLAGS = $(AM_LDFLAGS) -DWITH_MPI $(METIS_LIBS)
+libswiftsim_mpi_la_CFLAGS = $(AM_CFLAGS) $(MPI_FLAGS)
+libswiftsim_mpi_la_LDFLAGS = $(AM_LDFLAGS) $(MPI_LIBS) $(EXTRA_LIBS)
 libswiftsim_mpi_la_SHORTNAME = mpi