构建与g ++ -c标志相关的错误

时间:2013-08-15 18:38:28

标签: eclipse build g++

我不懂g ++ -c标志。基于定义:编译或汇编源文件,但不链接。链接阶段根本没有完成。最终输出是每个源文件的目标文件的形式。我需要帮助来了解导致后续构建过程的错误的原因。 感谢

我尝试在eclipse中编译示例helloworld程序。

#include <iostream>
using namespace std;

int main ()
{
  cout << "Hello World!";
  return 0;
}

没有-c。 eclipse给了我错误:

make all 
Building file: ../app.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -fmessage-length=0 -fPIC -MMD -MP -MF"app.d" -MT"app.d" -o "app.o" "../app.cpp"
Finished building: ../app.cpp

Building target: app.so
Invoking: GCC C++ Linker
g++ -shared -o "app.so"  ./app.o   
./app.o: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../lib64/crti.o:(.fini+0x0): first defined here
./app.o: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../lib64/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/crtendS.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
./app.o:(.dtors+0x8): first defined here
/usr/bin/ld: warning: Cannot create .eh_frame_hdr section, --eh-frame-hdr ignored.
/usr/bin/ld: error in ./app.o(.eh_frame); no .eh_frame_hdr table will be created.
collect2: ld returned 1 exit status
make: *** [app.so] Error 1

11:25:49 Build Finished (took 463ms)

使用-c,它构建正常:

11:33:16 **** Incremental Build of configuration Debug for project app ****
make all 
Building file: ../app.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -fPIC -MMD -MP -MF"app.d" -MT"app.d" -o "app.o" "../app.cpp"
Finished building: ../app.cpp

Building target: app.so
Invoking: GCC C++ Linker
g++ -shared -o "app.so"  ./app.o   
Finished building target: app.so

11:33:16建成(耗时311毫秒)

生成可执行文件的新修补程序

13:32:44 **** Incremental Build of configuration Debug for project app ****
make all 
Building file: ../app.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -fPIC -MMD -MP -MF"app.d" -MT"app.d" -o "app.o" "../app.cpp"
Finished building: ../app.cpp

Building target: app
Invoking: GCC C++ Linker
g++ -shared -o "app"  ./app.o   
Finished building target: app

1 个答案:

答案 0 :(得分:2)

在第一种情况下,如果没有-c,您的第一个g ++调用会生成一个误导性地命名为&#34; app.o&#34;的完全链接的可执行文件。 (尝试键入&#34; file ./app.o"在两种情况下描述文件。这可能很有趣。)您将能够运行它。 (输入./app.o)

使用-c标志,g ++调用只生成目标代码,而 适合于进一步的链接阶段(如您所见)。