g++ 输出:文件无法识别:文件格式无法识别 [英] g++ output: file not recognized: File format not recognized
问题描述
我第一次尝试用多个文件构建程序.我只用 main.cpp 编译程序从来没有遇到过任何问题.使用以下命令,结果如下:
I am trying to build program with multiple files for the first time. I have never had any problem with compliling program with main.cpp only. With following commands, this is the result:
$ g++ -c src/CNumber.cpp src/CNumber.h -o src/CNumber.o
$ g++ -c src/CExprPart.cpp src/CExprPart.h -o src/CExprPart.o
$ g++ -c src/CExpr.cpp src/CExpr.h -o src/CExpr.o
$ g++ -c src/main.cpp -o src/main.o
$ g++ src/CNumber.o src/CExprPart.o src/CExpr.o src/main.o -o execprogram
src/CNumber.o: file not recognized: File format not recognized
collect2: error: ld returned 1 exit status
什么会导致这样的错误,我该怎么办?使用带有 gcc 的 Linux Mint(Ubuntu/Linaro 4.7.2-2ubuntu1).谢谢
What could cause such error and what should I do with it? Using Linux Mint with gcc (Ubuntu/Linaro 4.7.2-2ubuntu1). Thank you
推荐答案
这是错误的:
g++ -c src/CNumber.cpp src/CNumber.h -o src/CNumber.o
您不应该编译".h 文件.这样做将创建预编译的头文件,这些文件不用于创建可执行文件.以上应该只是
You shouldn't "compile" .h files. Doing so will create precompiled header files, which are not used to create an executable. The above should simply be
g++ -c src/CNumber.cpp -o src/CNumber.o
类似编译其他 .cpp 文件
Similar for compiling the other .cpp files
这篇关于g++ 输出:文件无法识别:文件格式无法识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!