C++ Makefile-错误:文件被截断
我有一个简单的Makefile:C++ Makefile-错误:文件被截断,c++,makefile,g++,C++,Makefile,G++,我有一个简单的Makefile: CC=g++ CFLAGS= -Wall -std=c++11 -M -MF dependencyFileName.d -c objects = Table.o LimitedTable.o aDimension.o test.o edit: $(objects) g++ -o edit $(objects) test.o: LimitedTable.o Table.o aDimension.o test.cpp $(CC) $(CFLA
CC=g++
CFLAGS= -Wall -std=c++11 -M -MF dependencyFileName.d -c
objects = Table.o LimitedTable.o aDimension.o test.o
edit: $(objects)
g++ -o edit $(objects)
test.o: LimitedTable.o Table.o aDimension.o test.cpp
$(CC) $(CFLAGS) test.cpp -o test.o
LimitedTable.o: LimitedTable.cpp LimitedTable.hpp Table.o aDimension.o
$(CC) $(CFLAGS) LimitedTable.cpp -o LimitedTable.o
aDimension.o: aDimension.cpp aDimension.cpp Table.o
$(CC) $(CFLAGS) aDimension.cpp -o aDimension.o
Table.o: Table.cpp Table.hpp
$(CC) $(CFLAGS) Table.cpp -o Table.o
clean:
rm -f *.o
我得到了这个错误:
marius@marius-Lenovo-Y50-70 ~/Documents $ make clean
rm -f *.o
marius@marius-Lenovo-Y50-70 ~/Documents $ make edit
g++ -Wall -std=c++11 -M -MF dependencyFileName.d -c Table.cpp -o Table.o
g++ -Wall -std=c++11 -M -MF dependencyFileName.d -c aDimension.cpp -o aDimension.o
g++ -Wall -std=c++11 -M -MF dependencyFileName.d -c LimitedTable.cpp -o LimitedTable.o
g++ -Wall -std=c++11 -M -MF dependencyFileName.d -c test.cpp -o test.o
g++ -o edit Table.o LimitedTable.o aDimension.o test.o
Table.o: file not recognized: File truncated
collect2: error: ld returned 1 exit status
make: *** [edit] Error 1
谁能告诉我怎么了?
其中一个文件中的错误包含可能是导致此错误的原因吗?处理依赖项文件的方式存在一些问题,但首先: 我有一个简单的Makefile 不,你没有。样板代码的数量太多,向projet添加任何文件都需要再次手动编辑该makefile 您的Makefile应该归结为:
SRC := $(wildcard *.cpp)
OBJ := $(SRC:.cpp=.o)
DEP := $(OBJ:.o=.d)
CPPFLAGS := -MMD -MP
CXXFLAGS := -std=c++11 -Wall
edit: $(OBJ)
$(CXX) $^ -o $@
-include $(DEP)
clean:
$(RM) $(OBJ) $(DEP)
给你:
- 避免重复太多
- 充分利用make的隐式规则来节省时间
- 使用正确的内置变量,而不是覆盖错误的变量
- 正确处理依赖项文件创建并实际使用它们来防止手动重新编译
- 将
或.cpp
文件添加到项目时,无需编辑生成文件.hpp
而且,这应该可以解决你的问题发生此类错误(“文件被截断”)后,请不要忘记在尝试再次编译之前进行清理。“文件被截断”听起来像“文件意外结束”。如果您通过运行
g++-Wall-std=c++11-c Table.cpp-o Table.o
,而不运行make
,手动重新生成它会怎么样?如果你试着用一个简单的“你好,世界”main
?好的,我明天会试试看,看起来好多了,谢谢:)但通配符是什么意思?它是否占用当前目录中的所有源文件?是的,就是它,如果需要指定要编译的每个文件,可以删除它。您还可以指定相对路径,如$(通配符src/*.cpp)
。make将自动识别继承文件之间的依赖关系?因为依赖关系由编译器创建(通过-MMD-MP
开关)并包含在makefile中(通过-include$(DEP)
),是的,它将自动处理。