I'm having difficulty figuring out how to create two hard links to my target program. My target program is foo
, and I want to create two hardlinks to foo, one called baz
and the other called bar
. Currently my makefile looks like this:
CC = gcc
CFLAGS = -g -std=c99 -pedantic -Wall
HOME = /my/home/dir
SOURCES = main.c bar.c baz.c datastructure.c ${HOME}/addNodes.c
OBJECTS = $(SOURCES:.c=.o)
TARGET = foo
LN_F = ln -f
$(TARGET): $(OBJECTS)
${CC} ${CFLAGS} -o $@ $^
%.o : %.c
$(CC) $(CFLAGS) -c $< -o $@
bar: ${TARGET}
${LN_F} ${TARGET} bar
baz: ${TARGET}
${LN_F} ${TARGET} baz
The problem is that I now have to create hardlinks to foo manually, by entering "ln -s foo bar"
and "ln -s foo baz"
If you go with symbolic links:
TARGET = foo
AUX1 = bar
AUX2 = baz
LN_S = ln -s -f
all: ${TARGET} ${AUX1} ${AUX2}
${TARGET}: $(OBJECTS)
${CC} ${CFLAGS} -o $@ $^
${AUX1}: ${TARGET}
${LN_S} ${TARGET} ${AUX1}
${AUX2}: ${TARGET}
${LN_S} ${TARGET} ${AUX2}
If you go with hard links, you will use ln -f
(without the -s
). Note that once upon a (very) long time ago, the -f
was not supported by ln
(and in those days, -s
wasn't an option either — I'm talking about that long ago), and a rule like these linking rules would use rm -f
to remove the link before running the ln
command.
You can also write the link lines using generic macros $@
and $?
(these are the reliable, POSIX-supported macros):
${AUX2}: ${TARGET}
${LN_S} $? $@
This works sanely because there is only one prerequisite for ${AUX2}
; if there were more than one prerequisite, $?
would not work — be careful.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments