[英]Error in running the program in linux using g++
在Linux上使用g ++进行编译时,在函数main': main.cpp:(.text+0x42): undefined reference to
给出了错误main': main.cpp:(.text+0x42): undefined reference to
GradeBook :: GradeBook(std :: string)的main': main.cpp:(.text+0x42): undefined reference to
collect2:错误:ld返回1退出状态
我正在使用以下命令进行编译:
g++ -o gradebook.cpp gradebook.h main.cpp
// gradebook.cpp
#include <iostream>
using namespace std;
#include "gradebook.h"
GradeBook::GradeBook( string name )
{
setCourseName( name );
maximumGrade = 0;
}
void GradeBook::setCourseName( string name )
{
if ( name.length() <= 25 )
courseName = name;
else
{
courseName = name.substr( 0, 25 );
cout << "Name \"" << name << "\" exceeds maximum length (25).\n"
<< "Limiting courseName to first 25 characters.\n" << endl;
}
}
string GradeBook::getCourseName()`
{
return courseName;`
}
void GradeBook::displayMessage()
{
cout << "Welcome to the grade book for\n" << getCourseName() << "!\n" << endl;
}
int maximum( int, int, int );
int maximumGrade;`
void GradeBook::inputGrades()
{
int grade1;
int grade2;
int grade3;
cout << "Enter three integer grades: ";
cin >> grade1 >> grade2 >> grade3;
}
int GradeBook::maximum( int x, int y, int z )`
{
int maximumValue = x;
if ( y > maximumValue )
maximumValue = y;
if ( z > maximumValue )
maximumValue = z;
return maximumValue;
}
void GradeBook::displayGradeReport()
{
cout << "Maximum of grades entered: " << endl;
}
// gradebook.h
#include <string>
using namespace std;
class GradeBook
{
public:
GradeBook (string);
void setCourseName (string);
string getCourseName ();
void displayMessage();
void inputGrades ();
void displayGradeReport ();
int maximum (int, int, int);
private:
string courseName;
int maximumGrade;
};
// main.cpp
#include "gradebook.h"
int main (int argc, char*argv[])
{
GradeBook myGradeBook ("Introduction to C++");
return 0;
}
GCC的-o选项可让您指定可执行文件/库的名称,如下所示:
g++ -o foo.exe foo.cpp
您忘了在标志后添加名称,所以我假设它使用gradebook.cpp作为输出名称。
就您而言,应该是
g++ -o my_prog gradebook.cpp main.cpp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.