Hard linking within makefile

Apollo

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"

Jonathan Leffler

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Hard linking within makefile

From Dev

C++ Linking to .so file within makefile

From Dev

Makefile not linking required dependency

From Dev

Hard Linking Other Users' Files

From Dev

Linking within a page with Bootstrap

From Dev

Linking within the page

From Dev

Makefile not linking the necessary .hpp files

From Dev

Makefile linking: undefined reference to _exit

From Dev

Makefile Linking with shared library fails

From Dev

Makefile linking issue with stacked classes

From Dev

C: Creating static library and linking using a Makefile

From Dev

Basic makefile/linking/library issue: No such file or directory

From Dev

Linking Custom Library to Linux Kernel Module in the Makefile

From Dev

makefile linking does not work (although no error message)

From Dev

c++ MakeFile linking to SQlite library

From Dev

Makefile: how to detect changes within the makefile itself?

From Dev

Git commit from within a Makefile

From Dev

Linking so file within android ndk

From Dev

Linux: How does hard-linking to a directory work?

From Dev

General Linking for Android NDK Makefile, Android.mk and OCaml

From Dev

Linking against included platform lib and headers with Android Studio and custom makefile

From Dev

undefined references when linking with static library with OTL using a makefile

From Dev

C++ Makefile doesn't compile dependencies/ linking error?

From Dev

Makefile for linking OpenCV and Existing Library (Without using cmake)

From Dev

Makefile for linking opencv.framework (I'm not using cmake)

From Dev

Hard copy of array with instance of class within object

From Dev

Using PETSc on makefile within my user-defined makefile

From Dev

Makefile variable referencing within a nested loop

From Dev

Where to add the -lm flag within a makefile?

Related Related

  1. 1

    Hard linking within makefile

  2. 2

    C++ Linking to .so file within makefile

  3. 3

    Makefile not linking required dependency

  4. 4

    Hard Linking Other Users' Files

  5. 5

    Linking within a page with Bootstrap

  6. 6

    Linking within the page

  7. 7

    Makefile not linking the necessary .hpp files

  8. 8

    Makefile linking: undefined reference to _exit

  9. 9

    Makefile Linking with shared library fails

  10. 10

    Makefile linking issue with stacked classes

  11. 11

    C: Creating static library and linking using a Makefile

  12. 12

    Basic makefile/linking/library issue: No such file or directory

  13. 13

    Linking Custom Library to Linux Kernel Module in the Makefile

  14. 14

    makefile linking does not work (although no error message)

  15. 15

    c++ MakeFile linking to SQlite library

  16. 16

    Makefile: how to detect changes within the makefile itself?

  17. 17

    Git commit from within a Makefile

  18. 18

    Linking so file within android ndk

  19. 19

    Linux: How does hard-linking to a directory work?

  20. 20

    General Linking for Android NDK Makefile, Android.mk and OCaml

  21. 21

    Linking against included platform lib and headers with Android Studio and custom makefile

  22. 22

    undefined references when linking with static library with OTL using a makefile

  23. 23

    C++ Makefile doesn't compile dependencies/ linking error?

  24. 24

    Makefile for linking OpenCV and Existing Library (Without using cmake)

  25. 25

    Makefile for linking opencv.framework (I'm not using cmake)

  26. 26

    Hard copy of array with instance of class within object

  27. 27

    Using PETSc on makefile within my user-defined makefile

  28. 28

    Makefile variable referencing within a nested loop

  29. 29

    Where to add the -lm flag within a makefile?

HotTag

Archive