Patrick Patrick - 1 year ago 60
C Question

how to put a executable run file under a created folder?

My original directory structure is:

include/ ( const.h , map_reduce.h )

src/ ( main.c , map_reduce.c )


CFLAGS = -Wall -Werror
BINS = bin_mapreduce

all: $(BINS)

bin_mapreduce: bin/mapreduce

bin/mapreduce: mapreduce | bin

gcc $(CFLAGS) src/main.c -o bin/$@

mkdir $@

rm -f bin/mapreduce

rm -f bin/mapreduce

I want to create a bin folder if there is no one.

Then, compile main.c to executable mapreduce file and put it into bin folder.

However, for bin/$@, if I add bin/ before $@, it shows a error:

/usr/bin/ld: cannot open output file bin/mapreduce: No such file or

collect2: error: ld returned 1 exit status

Makefile:19: recipe for target 'mapreduce' failed

make: *** [mapreduce] Error 1

If do it without bin/, just :

gcc $(CFLAGS) src/main.c -o $@

If creates a mapreduce file outside the bin folder, the bin folder is created.

How can I put mapreduce file in the bin folder?

Answer Source
bin/mapreduce: mapreduce | bin

That rule is not correct. You need bin to be created before mapreduce is run. But that rule does not achieve that sequencing. Here are some options:

  • Put the order only prerequisite on the mapreduce target:

    mapreduce: | bin
  • Put the order only prerequisite on a $(BINS) target:

    $(BINS) : | bin