2

I am trying to create a Makefile with two targets: setup and clean. The setup target is supposed to find all the files in the /usr/lib/man-db and copy them to the directory proj/lib.

setup:
    files=( $( find /usr/lib/man-db -name "*.so" ) )
    mkdir proj; cd proj; mkdir lib bin
    cd
    cp $HOME/proj/lib "${files[@]}"
clean:
    rm -r proj
3

Your Makefile has two errors:

  1. It relies on bash (or on a shell with the same array syntax) without setting the make variable SHELL to /bin/bash (or whatever the path is to the shell executable file).

  2. It relies on each action being run in the same shell instance. Usually, each action line is invoked by a separate $SHELL (/bin/sh by default), which means that you can't set a shell variable on one line and then use it on another, or cd into one directory and then assume that you're still in the same directory on the next line of the Makefile.

I would probably rewrite the setup target as something like

setup:
    mkdir -p proj/lib proj/bin
    find /usr/lib/man-db -type f -name '*.so' -exec cp {} proj/lib \;

This additionally does not rely on the bash shell being the shell used by make, and it also corrects the ordering of the arguments to cp (you have yours back to front).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.