Fix 'duplicate symbol' error for tests using multiple static libs (OS X)
authorIvan Maidanski <ivmai@mail.ru>
Tue, 26 Feb 2019 21:48:12 +0000 (00:48 +0300)
committerIvan Maidanski <ivmai@mail.ru>
Tue, 26 Feb 2019 21:53:05 +0000 (00:53 +0300)
Issue #263 (bdwgc).

If configured with --enable-static, libtool passes libgc.a to gcc
twice (with a relative path and with an absolute one) when linking
tests that depend on libcord.a, libgccpp.a or libstaticrootslib_test.a
which, in turn, depends on libgc.a.  Double specification of libgc.a
seems to confuse ld tool of MacOS 10.14.

The workaround is to omit libgc.la in *_LDADD specification of the
mentioned tests when the shared libraries are not requested, at least.

* configure.ac (ENABLE_SHARED): New AM_CONDITIONAL.
* cord/cord.am (cordtest_LDADD): Do not add libgc.la unless
ENABLE_SHARED; add comment.
* tests/tests.am (staticrootstest_LDADD): Likewise.
* tests/tests.am [CPLUSPLUS && !AVOID_CPP_LIB] (test_cpp_LDADD):
Likewise.

configure.ac
cord/cord.am
tests/tests.am

index 1d92e2b..49af362 100644 (file)
@@ -964,6 +964,8 @@ AC_ARG_ENABLE(docs,
                         [do not build and install documentation])])
 AM_CONDITIONAL(ENABLE_DOCS, test x$enable_docs != xno)
 
+AM_CONDITIONAL(ENABLE_SHARED, test x$enable_shared = xyes)
+
 # Atomic Ops
 # ----------
 
index 50fa104..c2ea532 100644 (file)
@@ -19,7 +19,13 @@ libcord_la_SOURCES = \
 TESTS += cordtest$(EXEEXT)
 check_PROGRAMS += cordtest
 cordtest_SOURCES = cord/tests/cordtest.c
-cordtest_LDADD = $(top_builddir)/libgc.la $(top_builddir)/libcord.la
+cordtest_LDADD = $(top_builddir)/libcord.la
+
+## In case of static libraries build, libgc.a is already referenced in
+## dependency_libs attribute of libcord.la file.
+if ENABLE_SHARED
+cordtest_LDADD += $(top_builddir)/libgc.la
+endif
 
 EXTRA_DIST += \
         cord/tests/de.c \
index 19046e0..93f4b27 100644 (file)
@@ -50,8 +50,8 @@ TESTS += staticrootstest$(EXEEXT)
 check_PROGRAMS += staticrootstest
 staticrootstest_SOURCES = tests/staticrootstest.c
 staticrootstest_CFLAGS = -DSTATICROOTSLIB2
-staticrootstest_LDADD = $(test_ldadd) libstaticrootslib_test.la \
-                        libstaticrootslib2_test.la
+staticrootstest_LDADD = $(nodist_libgc_la_OBJECTS) $(EXTRA_TEST_LIBS) \
+                        libstaticrootslib_test.la libstaticrootslib2_test.la
 check_LTLIBRARIES += libstaticrootslib_test.la libstaticrootslib2_test.la
 libstaticrootslib_test_la_SOURCES = tests/staticrootslib.c
 libstaticrootslib_test_la_LIBADD = $(test_ldadd)
@@ -61,6 +61,9 @@ libstaticrootslib2_test_la_SOURCES = tests/staticrootslib.c
 libstaticrootslib2_test_la_LIBADD = $(test_ldadd)
 libstaticrootslib2_test_la_CFLAGS = -DSTATICROOTSLIB2
 libstaticrootslib2_test_la_LDFLAGS = -no-undefined -rpath /nowhere
+if ENABLE_SHARED
+staticrootstest_LDADD += $(top_builddir)/libgc.la
+endif
 
 if KEEP_BACK_PTRS
 TESTS += tracetest$(EXEEXT)
@@ -106,7 +109,13 @@ test_cpp_SOURCES = tests/test_cpp.cc
 if AVOID_CPP_LIB
 test_cpp_LDADD = gc_cpp.o $(test_ldadd) $(CXXLIBS)
 else
-test_cpp_LDADD = libgccpp.la $(test_ldadd) $(CXXLIBS)
+test_cpp_LDADD = libgccpp.la $(nodist_libgc_la_OBJECTS) \
+                $(EXTRA_TEST_LIBS) $(CXXLIBS)
+## In case of static libraries build, libgc.a is already referenced in
+## dependency_libs attribute of libgccpp.la file.
+if ENABLE_SHARED
+test_cpp_LDADD += $(top_builddir)/libgc.la
+endif
 endif
 endif