C语言 gcc - 文件无法识别:文件格式无法识别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13205865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
gcc - file not recognized: File format not recognized
提问by zee
I'm trying to get a small project compiled but am getting this error, I've been searching around and people get this error mainly because of incorrect file extension, but I don't really think that's the cause here:
我正在尝试编译一个小项目,但是遇到了这个错误,我一直在四处搜索,人们主要是因为文件扩展名不正确而收到这个错误,但我真的不认为这是这里的原因:
gcc -c -W -Wall -ggdb -I. router.c -o router.o
router.c:106: warning: unused parameter ‘hname'
router.c: In function ‘flood_neighbors':
router.c:464: warning: unused variable ‘bytes_rcvd'
router.c: At top level:
router.c:536: warning: unused parameter ‘fd'
gcc -c -W -Wall -ggdb -I. link_info.h -o link_info.o
gcc -c -W -Wall -ggdb -I. route.h -o route.o
gcc -c -W -Wall -ggdb -I. sequence.h -o sequence.o
gcc -W -Wall -ggdb -I. router.o link_info.o route.o sequence.o -o router
link_info.o: file not recognized: File format not recognized
collect2: ld returned 1 exit status
make: *** [router] Error 1
and my make file looks like:
我的制作文件看起来像:
CC = gcc
INC = -I.
FLAGS = -W -Wall -ggdb
router: router.o link_info.o route.o sequence.o
$(CC) $(FLAGS) $(INC) $^ -o $@
router.o: router.c
$(CC) -c $(FLAGS) $(INC) $< -o $@
sequence.o: sequence.h sequence.h
$(CC) -c $(FLAGS) $(INC) $< -o $@
link_info.o: link_info.h link_info.c
$(CC) -c $(FLAGS) $(INC) $< -o $@
route.o: route.h route.c
$(CC) -c $(FLAGS) $(INC) $< -o $@
What I'm confused on is the rules for three object files are of the same format, but why only the link one yells? Thanks a lot!
我感到困惑的是三个目标文件的规则格式相同,但为什么只有链接一个大喊大叫?非常感谢!
采纳答案by CCoder
I would like to suggest few edits in your makefile.
我想建议在您的 makefile 中进行一些编辑。
CC = gcc
INC = -I.
FLAGS = -W -Wall -ggdb
router: router.o link_info.o route.o sequence.o
$(CC) $(FLAGS) $(INC) $^ -o $@
router.o: router.c
$(CC) -c $(FLAGS) $(INC) $< -o $@
sequence.o: sequence.h sequence.h //Where is the c file ?
$(CC) -c $(FLAGS) $(INC) $< -o $@
link_info.o: link_info.h link_info.c //Change the order. Put c file first and then the header
$(CC) -c $(FLAGS) $(INC) $< -o $@
route.o: route.h route.c //Same as above
$(CC) -c $(FLAGS) $(INC) $< -o $@
回答by sashang
With recent versions of make you can remove all the object rules because make uses a default rule to build an object from .c or .cpp files. Just leave the rule to build the final executable in place.
使用最新版本的 make 您可以删除所有对象规则,因为 make 使用默认规则从 .c 或 .cpp 文件构建对象。只需保留构建最终可执行文件的规则即可。
For example, change the file so it contains this line only:
例如,更改文件使其仅包含以下行:
router: router.o link_info.o route.o sequence.o
$(CC) $(FLAGS) $(INC) $^ -o $@
回答by Mike Housky
Move link_info.o to the end of the list in the router:dependencies and I suspect you'll blow up on route.o instead.
将 link_info.o 移动到router:依赖项列表的末尾,我怀疑你会在 route.o 上炸毁。
You seem to be compiling a headers as C files. I have no idea what GCC does in that case. In your makefile rules, the .c file must be the first dependency if you are going to use $< to generate the source file to compile.
您似乎正在将头文件编译为 C 文件。我不知道 GCC 在这种情况下会做什么。在您的 makefile 规则中,如果您要使用 $< 生成要编译的源文件,则 .c 文件必须是第一个依赖项。
The link_info: and route: rules have the .c and .h files reversed and the sequence: rule lists the .h file twice!
link_info: 和 route: 规则将 .c 和 .h 文件颠倒了,而 sequence: 规则列出了 .h 文件两次!

