Makefile 中的 ShellScript

ShellScript in a Makefile

我编写了以下 makefile:

# make the iMe program
#

SDIR=src
IDIR=include
CFLAGS=-I$(IDIR)

ODIR=obj
EDIR=bin

LIBS=-lrt -lpthread

_DEPS = 1.h 2.h 3.h 4.h 5.h 6.h 7.h 8.h 9.h 10.h 11.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))

_OBJ = 1.o 2.o 3.o 4.o 5.o 6.o 7.o 8.o 9.o 10.o 11.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))


$(ODIR)/%.o: $(SDIR)/%.c $(DEPS)
    gcc -c -o $@ $< $(CFLAGS) -O3

$(EDIR)/iMe: $(OBJ)
    gcc -o $@ $^ $(CFLAGS) $(LIBS)

# Clean 

clean:
    -rm -f ./$(ODIR)/* $(OBJ) ./$(EDIR)/*

# Backup 

backup:
    tar -czvf backup_iMe_`date +%d-%m-%Y`.tar.gz *


# Run the program with first cenario
run_first:
./$(EDIR)/iMe ./tests/in/cenario1 ./tests/out/cenario1 -l cenario1.log -t 1000

#Run the program with second cenario
run_second:
./$(EDIR)/iMe ./tests/in/cenario2 ./tests/out/cenario2 -l cenario2.log -t 1000

虽然一切 运行 都很完美,但我想将 运行 部分分离到一个不同的文件中,称为 tests.sh,然后在 makefile 中调用它们。

像这样:

#!/bin/sh
# Run the program with first cenario
run_first:
./bin/iMe ./tests/in/cenario1 ./tests/out/cenario1 -l cenario1.log -t 1000

#Run the program with second cenario
run_second:
./bin/iMe ./tests/in/cenario2 ./tests/out/cenario2 -l cenario2.log -t 1000

但是这不起作用,因为我不知道如何在 makefile 中调用它...任何帮助将不胜感激。

谢谢!

您需要将选择作为参数传递给 tests.sh 脚本。这里是makefile

的内容
# make the iMe program
#

SDIR=src
IDIR=include
CFLAGS=-I$(IDIR)

ODIR=obj
EDIR=bin

LIBS=-lrt -lpthread

_DEPS = 1.h 2.h 3.h 4.h 5.h 6.h 7.h 8.h 9.h 10.h 11.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))

_OBJ = 1.o 2.o 3.o 4.o 5.o 6.o 7.o 8.o 9.o 10.o 11.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))


$(ODIR)/%.o: $(SDIR)/%.c $(DEPS)
    gcc -c -o $@ $< $(CFLAGS) -O3

$(EDIR)/iMe: $(OBJ)
    gcc -o $@ $^ $(CFLAGS) $(LIBS)

# Clean 

clean:
    -rm -f ./$(ODIR)/* $(OBJ) ./$(EDIR)/*

# Backup 

backup:
    tar -czvf backup_iMe_`date +%d-%m-%Y`.tar.gz *


./tests.sh 1
#./tests.sh 2

那么下面是tests.sh

的内容
#!/bin/sh
# Run the program with first scenario
scenario=
if [  -eq 1 ]
then
./bin/iMe ./tests/in/cenario1 ./tests/out/cenario1 -l cenario1.log -t 1000
elif [  -eq 2 ]
then
#Run the program with second scenario
#run_second:
./bin/iMe ./tests/in/cenario2 ./tests/out/cenario2 -l cenario2.log -t 1000
fi