C子目录规则以创建obj

C子目录规则以创建obj

本文介绍了Makefile C子目录规则以创建obj的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am running a simple Makefile with no problems:

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)

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

The files are in the main project's directory:

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

然后,我尝试整理文件,并放置以下内容:

Then I tried to organized the files, and put things like:

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

和其他子目录目录:

./project/lib
./project/obj

然后创建新版本的Makefile:

Then the new version of the 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)/*~

我正在使用带有gcc编译器的Emacs在Linux上进行编译:

I am compiling on Linux using Emacs with the gcc compiler:

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

然后,我在Emacs上运行:

Then, I run on 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文件中应缺少什么规则?

What rule should be missing to be included in the Makefile file?

所有评论和建议都将受到高度赞赏.

All comments and suggestions are highly appreciated.

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

Thanks for your suggestion, it is added to the code. Then the compiler complains:

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

还有其他建议吗?

提前谢谢!

推荐答案

要修复错误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').

This creates a rule for all objects in the $(OBJ) variable. The second parameter ('$(ODIR)/%.o') extracts the file name from the full path in order to pass just the file name to the third parameter ('src/%.c').

这篇关于Makefile C子目录规则以创建obj的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 19:57