【发布时间】:2017-02-12 13:45:45
【问题描述】:
---底部的完整 Makefile ---
我目前正在完成一个项目,所以我将进入打包/编译部分。 我正在使用 Make,我的项目的特殊性在于它包含两个项目,每个项目都有一个 main()。
我想要第一个项目的二进制文件(名为“shell”)和第二个项目的二进制文件(名为“ls”)。我已编辑 Makefile 以分隔目标、源文件等。以下是重要的几行:
-- Projects --
TARGET_SHELL = shell
TARGET_LS = ls
-- Directories --
SOURCE = ./src
BIN = ./bin
DIRLIST = ${SOURCE} ${BIN}
-- Targets --
BINSHELL = ${TARGET_SHELL:%=${BIN}/%}
BINLS = ${TARGET_LS:%=${BIN}/%}
-- Files --
SRC_SHELL = ${wildcard ${SOURCE}/execution.c ${SOURCE}/shell.c}
SRC_LS = ${wildcard ${SOURCE}/commande_ls.c}
INT_SHELL = ${wildcard ${SOURCE}/execution.h}
INT_LS = ${wildcard ${SOURCE}/commande_ls.h}
OBJ_SHELL = ${SRC_SHELL:%.c=%.o}
OBJ_LS = ${SRC_LS:%.c=%.o}
-- Rules --
all : ${BINSHELL} ${BINLS}
-- Binaries --
${BIN}/${TARGET_SHELL} : ${${TARGET_SHELL}:%=${SOURCE}/%}
${BIN}/${TARGET_LS} : ${${TARGET_LS}:%=${SOURCE}/%}
${BIN}/% : $(OBJ_SHELL)
@echo
@echo Linking bytecode : $@
@echo ----------------
@echo
${CC} -o $@ $^ ${LDFLAGS}
@echo
@echo Done
@echo
“make”命令完美运行。最后,我有两个二进制文件,一个名为“shell”,另一个名为“ls”。不错!
但实际上,这两个二进制文件完全一样,都是执行“shell”项目。我希望二进制“shell”执行“shell”项目,而名为“ls”的二进制文件执行“ls”项目...
我知道我必须编辑 Makefile 的结尾,但我不知道是什么:(
谢谢
#/// @file
#/// @brief Generic Makefile for the System 2 project.
#
#/// @detail If you just add some library files used by the project.c program, you have nothing to change to compile them if sources are in the ./src directory. To add a new binary, just add the name of the main file in the TARGETS variable.
#Nom du project
TARGET_SHELL = shell
TARGET_LS = ls
##############
# Constantes #
##############
# Repertoires
SOURCE = ./src
BIN = ./bin
DOCPATH = ${SOURCE}/dox
DOCTARGET = ./doc
DIRLIST = ${SOURCE} ${BIN}
#DEP = ${SOURCE}/depend
#DIRLIST = ${SOURCE} ${BIN} ${OPT} ${DEP}
# Cibles
BINSHELL = ${TARGET_SHELL:%=${BIN}/%}
BINLS = ${TARGET_LS:%=${BIN}/%}
# Commandes
CC = gcc
# Options
CFLAGS = -O0 -g -W -Wall -Wextra -Wconversion -Werror -mtune=native -march=native -std=c99 -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700
LDFLAGS = -lm -W -Wall -pedantic -L. -lm
# Fichiers
DOX = ${wildcard ${DOCPATH}/*.dox} # Sources
SRC_SHELL = ${wildcard ${SOURCE}/divers.c ${SOURCE}/commandes_externes.c ${SOURCE}/commandes_internes.c ${SOURCE}/entities.c ${SOURCE}/execution.c ${SOURCE}/parse.c ${SOURCE}/shell.c} # Sources
SRC_LS = ${wildcard ${SOURCE}/commande_ls.c}
INT_SHELL = ${wildcard ${SOURCE}/divers.h ${SOURCE}/commandes_externes.h ${SOURCE}/commandes_internes.h ${SOURCE}/execution.h ${SOURCE}/parse.h} # Interfaces
INT_LS = ${wildcard ${SOURCE}/commande_ls.h}
OBJ_SHELL = ${SRC_SHELL:%.c=%.o} # Objets
OBJ_LS = ${SRC_LS:%.c=%.o}
##########
# Regles #
##########
# ALL
all : ${BINSHELL} ${BINLS}
# CLEAN
clean :
@echo
@echo Cleaning : object files
@echo --------
@echo
rm -f ${OBJ_SHELL}
rm -f ${OBJ_LS}
clean-doc :
@echo
@echo Cleaning : object files
@echo --------
@echo
rm -fr ${DOCTARGET}
clean-emacs :
@echo
@echo Cleaning : emacs back-ups
@echo --------
@echo
rm -f ${SOURCE}/*~
rm -f ${SOURCE}/\#*\#
rm -f *~
rm -f \#*\#
clean-bin :
@echo
@echo Cleaning : binaries
@echo --------
@echo
rm -f ${BINSHELL}
rm -f ${BINLS}
distclean : clean clean-emacs clean-bin
dirs :
@for dir in ${DIRLIST} ;\
do \
echo Creating directory : $${dir} ;\
echo ------------------ ;\
if test -d $${dir} ;\
then \
echo Directory already exists ;\
else mkdir -p $${dir} ;\
fi ;\
echo Done ;\
echo ;\
done
# Binaires
${BIN}/${TARGET_SHELL} : ${${TARGET_SHELL}:%=${SOURCE}/%}
${BIN}/${TARGET_LS} : ${${TARGET_LS}:%=${SOURCE}/%}
${BIN}/% : $(OBJ_SHELL)
@echo
@echo Linking bytecode : $@
@echo ----------------
@echo
${CC} -o $@ $^ ${LDFLAGS}
@echo
@echo Done
@echo
# Regles generiques
%.o : %.c %.h
@echo
@echo Compiling $@
@echo --------
@echo
$(CC) $(CFLAGS) -c $< -o $@
# Documentation
doc : ${SRC} ${INT} ${DOX}
doxygen; doxygen
#############################
# Inclusion et spécificités #
#############################
.PHONY : all clean clean-doc clean-emacs clean-bin distclean doc
【问题讨论】:
-
你没有意义的东西,如果你精确一个文件,为什么要使用通配符?这不是一个有效的 makefile。
-
是的,我忘了删除通配符,谢谢。而且我的文件是有效的,我只是没有把它的全部内容放在我的帖子上。
-
-- Projects --不是有效评论 -
我知道这只是为了这篇文章,让我的 Makefile 更具可读性:)
-
如果您愿意,请使用正确的方式来注释 makefile
# -- Projects --,但不要使您的 makefile 无效。