这是一个在makefile中root的好方法吗?
SHELL = /bin/sh INSTDIR = /usr/bin/ OBJS = main.o file.o gen.o stat.o program1: $(OBJS) gcc -o program1 $(OBJS) main.o: main.c file.h gen.h stat.h gcc -c main.c file.o: file.c file.h gcc -c file.c gen.o: gen.c gen.h gcc -c gen.c stat.o: stat.c stat.h gcc -c stat.c clean: rm -f $(OBJS) program1 install: @if [ -f program1 ]; then \ if [ $$(id -u) -eq 0 ]; then \ cp program1 $(INSTDIR) && \ echo "Installed in $(INSTDIR)" ; \ else \ (sudo cp program1 $(INSTDIR) 2> /dev/null && \ echo "Installed in $(INSTDIR)") || \ (echo 'No sudo on this machine, trying su.' \ && su -c "cp program1 $(INSTDIR)" && \ echo "Installed in $(INSTDIR)") ; \ fi ; \ else \ echo "There was no program to install, run make." ; \ fi uninstall: @if [ -f $(INSTDIR)program1 ]; then \ if [ $$(id -u) -eq 0 ]; then \ rm $(INSTDIR)program1 && \ echo "Uninstalled in $(INSTDIR)" ; \ else \ (sudo rm $(INSTDIR)program1 2> /dev/null && \ echo "Uninstalled in $(INSTDIR)") || \ (echo 'No sudo on this machine, trying su.' \ && su -c "rm $(INSTDIR)program1" && \ echo "Uninstalled in $(INSTDIR)") ; \ fi ; \ else \ echo "There was no program to remove." ; \ fi
答案 0 :(得分:19)
首先,用户通常sudo make install
。其次,您没有考虑将程序安装到自定义目录中的情况,该目录并不总是需要root权限。例如,打包工具使用此功能,并且它们没有root权限。