Include library path in the makefile

makefile

My makefile looks as follows

program_NAME := myprogram
program_C_SRCS := $(wildcard *.cc)
program_C_OBJS := ${program_C_SRCS:.cc=.o}
program_OBJS := $(program_C_OBJS) 
program_INCLUDE_DIRS := ../INCLUDE
program_LIBRARY_DIRS := 
program_LIBRARIES :=
CPPFLAGS += $(foreach includedir,$(program_INCLUDE_DIRS),-I$(includedir))
LDFLAGS += $(foreach librarydir,$(program_LIBRARY_DIRS),-L$(librarydir))
LDFLAGS += $(foreach library,$(program_LIBRARIES),-l$(library))

.PHONY: all clean distclean

all: $(program_NAME)

$(program_NAME): $(program_OBJS)
$(LINK.cc) $(program_OBJS) -o $(program_NAME)

clean:
    @- $(RM) $(program_NAME)
    @- $(RM) $(program_OBJS)

distclean: clean

I have created a library stack.a in some path /home/Desktop/kk/stack.
I want to include this library into my makefile so that during linking it should be picked up from that path.

I tried to give:

program_LIBRARY_DIRS := /home/Desktop/kk/stack

and in linking step I gave:

$(LINK.cc) $(program_OBJS) stack.a -o $(program_NAME)

But the makefile is not able to pick up the library from the path mentioned.

Instead, if I directly give:

$(LINK.cc) $(program_OBJS) /home/Desktop/kk/stack/stack.a -o $(program_NAME)

it works perfectly.

Please help me how to include this library path so that I do not have to give the location of library in link command.

Best Solution

The -L link option only applies to libraries named libX.a or libX.so and linked using -lX, for some string X. You would need to rename stack.a to libstack.a and refer to it in the link command as -lstack.

(You should also, ideally, put that in the program_LIBRARIES definition and use that in the $(LINK.cc) line.)