Search code examples
gccmakefilegnu-make

Check for library existence in Makefile


Is there a way to check in Makefile whether library (here: librt) is available in a system? For older systems with librt being in library, not in glibc, I want to test whether it is in a system, if so then link it, if not, then not (as it's not needed).

In other words, in bash it would be something along those lines:

if [ -f /usr/lib/librt.so ] || [ -f /usr/lib/librt.a ]; then
    LIBS=${LIBS} -lrt
fi

But how to do it in Makefile?


Solution

  • Although IMO it's not very useful, you could use this to get what you asked for:

    RTLIB_PATHS = /usr/lib/librt.so /usr/lib/librt.a
    RTLIB := $(if $(wildcard $(RTLIB_PATHS)),-lrt)
    
    LIBS += $(RTLIB)