简体   繁体   English

g ++合并:ld:致命:文件main.o:未知文件类型

[英]g++ consolidation: ld: fatal: file main.o: unknown file type

I'm scanning the web and all my project files for solution but still can't find the answer why my linker won't finish the job. 我正在扫描Web和所有项目文件以寻求解决方案,但仍然找不到答案,为什么我的链接器无法完成工作。 Everything smoothly compiles into .o files, but the last make command fails. 一切都顺利地编译成.o文件,但最后一个make命令失败。 And here is the Makefile content: 这是Makefile的内容:

CXX = g++
CXXFLAGS = -Wall -pedantic -c
OBJS = main.o operacje.o porownaj.o 
dzialania: $(OBJS)
    $(CXX) $^ -o $@

main.o: main.cpp operacje.h porownaj.h
    $(CXX) $(CXXFLAGS) $^ -o $@

operacje.o: operacje.cpp operacje.h porownaj.h
    $(CXX) $(CXXFLAGS) $^ -o $@

porownaj.o: porownaj.cpp operacje.h porownaj.h
    $(CXX) $(CXXFLAGS) $^ -o $@

clean:
    rm -f *o

and again, here is the mistake that pops out: 再一次,这是弹出的错误:

g++ main.o operacje.o porownaj.o -o dzialania
ld: fatal: file main.o: unknown file type
ld: fatal: file processing errors. No output written to dzialania
*** Error code 1
make: Fatal error: Command failed for target `dzialania'

I'm sure it's some kind of a basic mistake but after staring at the file for a few hours I won't notice it anyway. 我确定这是一个基本错误,但是在盯着文件几个小时后,我还是不会注意到它。 Maybe some of you folks with notice the bug with a fresh eye. 也许你们中的一些人以崭新的眼光注意到了这个错误。

btw. 顺便说一句。 it's my first post after long-term passive lurking, I hope I did everything right. 这是我长期潜伏后的第一篇文章,希望我做对了所有事情。 Thanks in advance! 提前致谢!

@edit1 OK, I did all the suggested corrections @ edit1好,我做了所有建议的更正

@edit2 Seems like the problem is caused by improper module division of my program. @ edit2似乎问题是由于我程序的模块划分不正确引起的。 I'll rearrange it's structure and let you know if it works then. 我将重新排列它的结构,然后告诉您它是否有效。 Thanks for all the support! 感谢所有的支持!

@edit3 OK, I changed the structure of my program and everything runs smooth, Thanks again! @ edit3好,我更改了程序的结构,一切运行顺利,再次感谢!

Try using $< instead of $^ in your rules to compile main.o , operacje.o , and porownaj.o : 尝试在规则中使用$<而不是$^来编译main.ooperacje.oporownaj.o

main.o: main.cpp operacje.h porownaj.h
    $(CXX) $(CXXFLAGS) $< -o $@

operacje.o: operacje.cpp operacje.h porownaj.h
    $(CXX) $(CXXFLAGS) $< -o $@

porownaj.o: porownaj.cpp operacje.h porownaj.h
    $(CXX) $(CXXFLAGS) $< -o $@

That will cause make to compile only the corresponding .cpp file. 这将导致make仅编译相应的.cpp文件。 When you use $^ the header files are passed to the g++ command which tells the compiler to create precompiled headers for them - that's what's ending up in main.o instead of the object file for main.cpp . 当您使用$^ ,头文件将传递到g++命令,该命令告诉编译器为其创建预编译的头-这就是main.o而不是main.cpp的目标文件的结尾。

GNU make variable definitions like CC = g++ , or CFLAGS = -Wall -pedantic etc.. should each be on its own line: GNU make变量定义,例如CC = g++CFLAGS = -Wall -pedantic等。应分别位于单独的行:

CC = g++ 
CFLAGS = -Wall -pedantic 
OBJS = main.o operacje.o porownaj.o

BTW, you probably mean 顺便说一句,你可能是说

CXX = g++
CXXFLAGS = -Wall -pedantic

You certainly don't want -c explicitly in your CFLAGS or CXXFLAGS ; 当然,您当然不需要-c在您的CFLAGSCXXFLAGS you really should remove it. 您确实应该删除它。

Also, recipes should be after its rule, so you want 另外,食谱应遵循其规则,因此您需要

dzialania: $(OBJS)  
    $(LINK.cc) $^ -o $@
operacje.o: operacje.cpp operacje.h porownaj.h  
    $(CXX) $(CXXFLAGS) -c $< -o $@

The several spaces are actually a single tab character. 这几个空格实际上是一个制表符。

Run make -p to understand the rules known by make ; 运行make -p以了解make已知的规则; see also this answer and that one . 也看到这个答案那一个

Take time to read GNU make documentation . 花一些时间阅读GNU make文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM