(.text+0x20): undefined reference to `main' 是什么意思

what does (.text+0x20): undefined reference to `main' mean

有人可以帮我解决我在下面的代码清单中遇到的错误吗?

错误很可能是关于 constructor/destructor:

/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':

(.text+0x20): undefined reference to `main'

collect2: error: ld returned 1 exit status

代码清单:

                         // counter_id.cpp
                        // Obco class member-function  definitions.
   #include <iostream>

   #include "counter_id.h"  // include counter class definition

   using namespace std;

    // constructor sets object's ID number and descriptive message
   Obco::Obco( int ID, string messageString )
  : objectID( ID ), message( messageString )
     {
   cout << "Object " << objectID << "constructor runs"
     << message << endl;
     } // end CreateAndDestroy constructor


     // destructor
    Obco::~Obco()
   {
   // output newline for certain objects; helps readability
     cout << ( objectID == 1 || objectID == 6 ? "\n" : "" );
     cout << "Object " << objectID << " destructor runs "
     << message << endl;
       } // end ~Obco destructor

您不只是单独编译 (-c),您正在尝试执行完整的 link,但是 linker 没有找到任何 main 函数。

要获得完整的 link,您需要在 g++ 调用中包含所有源文件或目标文件(尤其是带有 main 函数的调用)。

如果只单独编译一个文件(稍后 linked),您需要添加 -c 选项。