I'm using automake to build my project. I have some third-party (open source) libraries as git submodules in my project that I want to individually build and link. Here's an edited (names changed) version of my Makefile.am:
lib_LTLIBRARIES = libfoo.la libbar.la
libbar_la_SOURCES = ../submodules/bar/bar.c
libfoo_la_LIBADD = libbar.la
libfoo_la_SOURCES = \
some_source.c \
some_other_source.c
libfoo_la_CFLAGS = $(CFLAGS)
libfoo_la_LDFLAGS = $(LIBS)
if OS_LINUX
libfoo_la_SOURCES += \
linux/some_source.c \
linux/some_other_source.c
libfoo_la_CFLAGS += $(PTHREAD_CFLAGS)
libfoo_la_LDFLAGS += $(PTHREAD_LIBS)
endif
if OS_WINDOWS_MSYS
libfoo_la_SOURCES += \
nt/some_source.c \
nt/some_other_source.c
libfoo_la_LDFLAGS += -no-undefined
endif
bin_PROGRAMS = main
main_SOURCES = main.c
main_LDADD = libfoo.la
autoreconf, configure and make run normally but make install fails with
/usr/bin/ld: cannot find -lbar
collect2: error: ld returned 1 exit status
It seems autoconf is trying to use libbar as a global, installed library instead of a local one? LDADD on the main target works fine though.
autoreconf -V outputs
autoreconf (GNU Autoconf) 2.71
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+/Autoconf: GNU GPL version 3 or later
<https://gnu.org/licenses/gpl.html>, <https://gnu.org/licenses/exceptions.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by David J. MacKenzie and Akim Demaille.
EDIT: I am on Linux. Don't mind the windows parts.
I tried to reproduce this on https://github.com/ndim/stackoverflow-q70584133 by basically copying the
Makefile.amyou have given and adding a few very basic source files.I found that building on Linux for Linux (Fedora 35, autoconf 2.69-37, automake 1.16.2-5, libtool 2.4.6-42) worked fine. Also running the
make installed result:So if you have problems building on and for Linux, something else must be off.
I presume you have run
make distcleanand then re-runautoreconfandconfigureto make sure your buildsystem and source tree and build tree are in a well defined state.However, building on Fedora 35 for Windows (both 32 and 64bit) failed for me when building
libbar.lawithout-no-undefined:Adding a line to
Makefile.amlikefixed the linking for Windows problem and made the whole thing build and run (built on Linux, run on Linux using wine):
(And BTW,
LIBSand_LIBStype variables should probably be added to a_LIBADDor_LDADDvariable, not to a_LDFLAGSvariable.)UPDATE
Building on Debian 10 (autoconf 2.69-11, automake 1:1.16.1-4, libtool 2.4.6-9), does produce a linker error when
main_LDADDis missinglibbar.laandsrc/main.ccalls thebar_func()function fromlibbar(uncomment the#definein src/main.c to reproduce):However, after removing the direct calls to
libbar'sbar_func()fromsrc/main.c, themakecommand will work again, as doesmake install:This suggests that calling a function from
libbarfrom a linking unit without explicitly linking that unit againstlibbaris an error, and that does make sense.So I still cannot reproduce OP's report of
makeworking butmake installfailing. OP is using newer autoconf (2.71) than I do (2.69). Perhaps OP is using different automake/libtool versions as well with different set of bugs (dpkg -l autoconf automake libtool,rpm -q autoconf automake libtool, etc.)?