Eclipse 与g+相关的生成错误+-c旗

Eclipse 与g+相关的生成错误+-c旗,eclipse,build,g++,Eclipse,Build,G++,我不懂g++-c标志。基于定义:编译或汇编源文件,但不链接。链接阶段根本没有完成。最终输出是每个源文件的对象文件形式。我需要帮助了解是什么原因导致以下生成过程中出现错误。 谢谢 我尝试在eclipse中编译示例helloworld程序 #include <iostream> using namespace std; int main () { cout << "Hello World!"; return 0; } 使用-c,它可以构建良好的: 11:33:16

我不懂g++-c标志。基于定义:编译或汇编源文件,但不链接。链接阶段根本没有完成。最终输出是每个源文件的对象文件形式。我需要帮助了解是什么原因导致以下生成过程中出现错误。 谢谢

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

#include <iostream>
using namespace std;

int main ()
{
  cout << "Hello World!";
  return 0;
}
使用-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构建完成(耗时311ms)

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

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

在第一种情况下,如果没有-c,您的第一个g++调用将生成一个完全链接的可执行文件,该文件被错误地命名为“app.o”。(尝试键入“file./app.o”来描述这两种情况下的文件。这可能很有趣。)您将能够运行它。(类型./app.o)


使用-c标志,该g++调用只会生成目标代码,并且适合进一步的链接阶段(如您所观察的)。

作为一个侧节点,共享库不应包含main()函数。您通常会在应用程序中创建一个main()函数,并在库中链接其他内容。哎呀,我刚刚发现我意外地将其设置为共享库模式。我已将此app.o修复为可执行应用程序文件。谢谢,但我还是不明白为什么没有-c标志我们就不能编译。我建议你读一下这样的内容:我发现Eclipse上有问题。我可以在命令行下使用Makefile很好地编译程序。如果我没有键入-c,程序只会给我与g++手册上的定义相匹配的对象文件。在eclipse中,我不知道为什么它在获取对象后没有停止。它进入链接阶段并生成错误。
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