Makefile 将程序移动到特定目录

Makefile to move programs to specific directory

我有一个我个人没有写的makefile,而且我不太擅长bash脚本和makefile,所以请原谅我事先缺乏知识;

如标题所述,我只是想在编译到 ../bin/ 文件夹时移动我的可执行文件。下面给出了我对此的尝试(无耻地从另一个 post 此处复制)如下所示(即我尝试进行一个应该移动文件的虚假安装,但可惜它没有。”

CXX = g++
CC  = g++


# Define preprocessor, compiler, and linker flags. Uncomment the # lines
# if you use clang++ and wish to use libc++ instead of libstd++.
CPPFLAGS  = -std=c++11 -I..
CXXFLAGS  = -g -O2 -Wall -W -pedantic-errors
CXXFLAGS += -Wmissing-braces -Wparentheses -Wold-style-cast 
CXXFLAGS += -std=c++11 
LDFLAGS   = -g -L..
MV = mv
PROG_PATH = ../bin/
#CPPFLAGS += -stdlib=libc++
#CXXFLAGS += -stdlib=libc++
#LDFLAGS +=  -stdlib=libc++

# Libraries
#LDLIBS = -lclientserver
# Targets
PROGS = myserver myclient libclientserver.a   
all: $(PROGS)
# Targets rely on implicit rules for compiling and linking
 # The dependency on libclientserver.a is not defined.
myserver: myserver.o messagehandler.o server.o connection.o database_memory.o database_file.o 
myclient: myclient.o connection.o server.o messagehandler.o
libclientserver.a: connection.o server.o
    ar rv libclientserver.a  connection.o server.o
    ranlib libclientserver.a

# Phony targets
.PHONY: all install clean

all: $(PROGS) install
install: $(MV) $(PROGS) $(PROG_PATH)




# Standard clean
clean:
rm -f *.o $(PROGS)

# Generate dependencies in *.d files
%.d: %.cc
    @set -e; rm -f $@; \
     $(CPP) -MM $(CPPFLAGS) $< > $@.$$$$; \
     sed 's,\($*\)\.o[ :]*,.o $@ : ,g' < $@.$$$$ > $@; \
     rm -f $@.$$$$

# Include the *.d files
 SRC = $(wildcard *.cc)
include $(SRC:.cc=.d)

那么我最好怎么做呢?编译器说

 make: *** No rule to make target `mv', needed by `install'.  Stop.

makefile 规则由两部分组成,规则依赖项的声明和要调用的命令。

依赖项列在规则的第一行冒号后,要执行的命令列在后续行中,均以制表符缩进。

您的安装规则需要取决于您正在移动的程序,可能还有目标目录(您可能需要一个创建目标的规则),而不是 mv 实用程序本身,因为您不需要需要构建它。

install: $(PROGS)
    mv $(PROGS) $(PROG_PATH)

请注意,虽然我使用了四个空格,但缩进必须是制表符。由于您(还没有?)制定 PROG_PATH 的规则,我已将其排除在依赖项列表之外。

另请注意,根据此规则,如果您调用 make 两次,make 将不得不重建您的程序,因为它们将被移动。你想考虑使用 cpinstall 而不是 mv.