sis*_*o s -3 c++ linker-errors
有人可以帮助我解决下面代码清单中出现的错误吗?
该错误很可能与构造函数/析构函数有关:
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/Scrt1.o: 在函数 `_start' 中:
(.text+0x20):对“main”的未定义引用
collect2:错误:ld 返回 1 退出状态
代码清单:
// 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
Run Code Online (Sandbox Code Playgroud)
您不仅仅是单独编译(-c),您正在尝试进行完整链接,但链接器尚未找到任何main函数。
对于完整的链接,您需要在调用中包含所有源文件或目标文件g++(尤其是带有您的main函数的文件)。
要单独编译一个文件(稍后链接),您需要添加该-c选项。