使用makefile C子目录规则来生成obj

时间:2012-10-03 14:55:58

标签: c compilation compiler-errors makefile

我正在运行一个没有问题的简单Makefile:

CC=gcc
CFLAGS= -std=c99 -ggdb -Wall -I.
DEPS = hellomake.h
OBJ = hellomake.o hellofunc.o 

%.o: %.c $(DEPS)
    $(CC) -c -o $@ $< $(CFLAGS)

hellomake: $(OBJ)
    gcc -o $@ $^ $(CFLAGS)

文件位于主项目的目录中:

./project/Makefile
./project/hellomake.c
./project/hellomake.h

然后我尝试组织文件,然后输入:

./project/Makefile
./project/src/hellomake.c
./project/include/hellomake.h

和额外的子目录目录:

./project/lib
./project/obj

然后是新版本的Makefile:

IDIR =include
CC=gcc
CFLAGS= -std=c99 -ggdb -Wall -I$(IDIR)

ODIR=obj
LDIR =lib

LIBS=-lm

_DEPS = hellomake.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))

_OBJ = hellomake.o hellofunc.o 
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))


$(ODIR)/%.o: %.c $(DEPS)
    $(CC) -c -o $@ $< $(CFLAGS)

hellomake: $(OBJ)
    gcc -o $@ $^ $(CFLAGS) $(LIBS)

.PHONY: clean

clean:
    rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~ 

我正在使用Emacs和gcc编译器在Linux上进行编译:

$ gcc --version
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3

然后,我在Emacs上运行:

<Esc> 
x
compile
make

它给出了信息:

"./project/src/" -*-
make: *** No rule to make target `obj/hellomake.o', needed by `hellomake'.  Stop.
Compilation exited abnormally with code 2 at Wed Oct  3 17:10:01

Makefile文件中应该包含哪些规则?

我们非常感谢所有意见和建议。


感谢您的建议,它已添加到代码中。然后编译器抱怨:

make -k 
make: *** No rule to make target `src/hellomake.c', needed by `obj/hellomake.o'.
make: *** No rule to make target `../include/hellomake.h', needed by `obj/hellomake.o'.
make: Target `obj/hellomake.o' not remade because of errors

其他一些建议?

提前致谢!

2 个答案:

答案 0 :(得分:8)

修复错误make: *** No rule to make target 'obj/hellomake.o', needed by 'hellomake'. Stop.

更改此行:

$(ODIR)/%.o: %.c $(DEPS) 

要:

$(OBJ): $(ODIR)/%.o: src/%.c $(DEPS)

这会为$(OBJ)变量中的所有对象创建规则。第二个参数('$(ODIR)/%.o')从完整路径中提取文件名,以便仅将文件名传递给第三个参数('src/%.c')。

答案 1 :(得分:-1)

确定。现在我正在尝试另一个例子[How can I create a Makefile for C projects with SRC, OBJ, and BIN subdirectories?],这里就是:

TARGET   = hi.sh
CC       = gcc
# compiling flags here
CFLAGS   = -std=c99 -ggdb -Wall -I./src
TARGET   = bin/hi.sh

LINKER   = gcc -o
# linking flags here
LFLAGS   = -Wall -I. -lm

# change these to set the proper directories where each files shoould be
SRCDIR   = src
OBJDIR   = obj
BINDIR   = bin

SOURCES  := $(wildcard $(SRCDIR)/*.c)
INCLUDES := $(wildcard $(SRCDIR)/*.h)
OBJECTS  := $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o)
rm       = rm -f


$(BINDIR)/$(TARGET): $(OBJECTS)
    @$(LINKER) $(TARGETPATH)/$(TARGET) $(LFLAGS) $(OBJECTS)
    @echo "Linking complete!"

OBJECTS  := $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o)

$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.c
    $(CC) $(CFLAGS) -c $< -o $@
    @echo "Compiled "$<" successfully!"

.PHONEY: clean
clean:
    @$(rm) $(OBJECTS)
    @echo "Cleanup complete!"

.PHONEY: remove
remove: clean
    @$(rm) $(BINDIR)/$(TARGET)
    @echo "Executable removed!"

文件组织为:

./project/bin/ executable
./project/ojb/*.0
./project/src/*.c and *.h
./project/Makefile

编译器坚持只提出一个投诉:

make -k 
/usr/bin/ld: cannot open output file /bin/hi.sh: Permission denied
collect2: ld returned 1 exit status
make: *** [bin/bin/hi.sh] Error 1

非常感谢所有意见和建议!