我运行一个简单的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)/*~
我正在使用带有gcc编译器的Emacs在Linux上进行编译:
$ gcc --version gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
然后,我在Emacs上运行:
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
其他一些建议?
提前致谢!
要修复错误make: *** No rule to make target 'obj/hellomake.o', needed by 'hellomake'. Stop.
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
‘)。
好。 现在我正在尝试另一个在此处找到的示例[ 如何使用SRC,OBJ和BIN子目录为C项目创建Makefile? ]在这里:
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
非常感谢所有的意见和建议!
以上就是c/c++开发分享obfile的Makefile C子目录规则相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/560392.html