Search code examples
cmakefilegnuworking-set

variable in makefile, unexpected behaviour


I have makefile where I have noticed somethign peculiar.

On the line marked with <------, if I put $(OBJECTS) instead of String.o the compilation fails. But I defined OBJECTS=$(String.o), why does that work this way?

LIBS=-ldl $(OPTLIBS)
PREFIX?=string-automaton/
DESTDIR=/home/jenia/learn-c-the-hard-way/lib2/
CFLAGS=-g -O2 -Wall -Wextra -rdynamic -DNDEBUG -Llibstrl


OBJECTS=$(String.o)

TARGET=build/lib_String.a
SO_TARGET=$(patsubst %.a,%.so,$(TARGET))

# The Target Build
all: build $(TARGET) $(SO_TARGET) 

$(TARGET): CFLAGS += -fPIC
$(TARGET): $(OBJECTS) 
    ar rcs $@ $(OBJECTS)   
    ranlib $@

$(SO_TARGET): $(TARGET) $(OBJECTS)   
    $(CC) -shared -o $@  String.o     # <----------- $(OBJECTS) woud fail

build:
    @mkdir -p build
    @mkdir -p bin

clean:
    rm -rf build $(OBJECTS) $(TESTS)
    rm -f tests/tests.log
    find . -name "*.gc*" -exec rm {} \;
    rm -rf `find . -name "*.dSYM" -print`

# The Install
install: all
    install -d $(DESTDIR)/$(PREFIX)/lib/
    install $(TARGET) $(DESTDIR)/$(PREFIX)/lib/

Also, as a follow up question, if I add $(OBJECTS) to the line <----- and remove $(OBJECTS) from the line before it then it compiles. Like this:

$(SO_TARGET): $(TARGET) 
$(CC) -shared -o $@ $(OBJECTS)    

So can someone please answer me why $(OBJECTS) doesn't resolve to String.o on line <-----?

Thanks


Solution

  • It is because $(String.o) will resolve to another string -- probably an empty string if String.o is not defined.

    I guess you should replace

    OBJECTS=$(String.o)
    

    with

    OBJECTS=String.o