问:
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
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
答:
This is wrong:
g++ -c src/CNumber.cpp src/CNumber.h -o src/CNumber.o
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
Similar for compiling the other .cpp files

博主首次构建多文件程序时遇到编译问题,之前仅用main.cpp编译程序无问题,使用特定命令后出现错误,询问原因及解决办法。回复指出不应编译.h文件,编译.h文件会创建预编译头文件,无法用于创建可执行文件。
5757

被折叠的 条评论
为什么被折叠?



