【问题标题】:architecture x86_64 error in c++C++ 中的架构 x86_64 错误
【发布时间】:2013-12-07 10:46:30
【问题描述】:

当我尝试在 mac 上使用 g++ 运行此 c++ 代码时,我收到以下错误。谁能告诉我这是什么意思。我是编程新手。因此,任何帮助将不胜感激。我正在尝试使用抽象类(Expr.cpp)和子类(Binary.cpp)来做到这一点。

我使用g++ main.cpp编译

错误 -

Undefined symbols for architecture x86_64:
"Expr::Expr()", referenced from:
  stepexecution(double, double, double)in cc8Vni4M.o
 "Expr::setArg(double)", referenced from:
  stepexecution(double, double, double)in cc8Vni4M.o
"Expr::setRece(double)", referenced from:
  stepexecution(double, double, double)in cc8Vni4M.o
"Binary::sum()", referenced from:
  stepexecution(double, double, double)in cc8Vni4M.o
"Expr::Expr()", referenced from:
  Binary::Binary()in cc8Vni4M.o
 ld: symbol(s) not found for architecture x86_64
 collect2: ld returned 1 exit status

代码

main.cpp -

 #include <iostream>
#include <fstream>
#include <string>

using namespace std;

void stepexecution(double x, double y, double z)
{
string STRING;
Expr ex;
Binary bi;
ifstream infile;
char ele1;
char ele2;
char rece;
double d = 7;
infile.open ("program.txt");
while(!infile.eof()) // To get you all the lines.
{
    getline(infile,STRING);
    int length = STRING.length();

        if(length>8)
        {
            rece = STRING.at(0);
            ele1 = STRING.at(4);
            ele2 = STRING.at(8);
            cout << ele1 << ele2;
            if(ele1=='x' && ele2=='y')
            {
                ex.setArg(x);
                ex.setRece(y);
                double a = bi.sum();
                cout << a;
            }

        }


}
infile.close();
}

int main()
{
char c;
int t = 0;
double x = 10;
double y = 0;
double z = 0;
while(t==0)
{
    cout << "Please enter a command:";
    cin >> c;
    if(c=='r')
    {
        x=0;
        y=0;
        z=0;
    }
    if(c=='x')
    {
        cout << "Please enter the value of x:";
        cin >> x;
    }
    if(c=='y')
    {
        cout << "Please enter the value of x:";
        cin >> y;
    }
    if(c=='z')
    {
        cout << "Please enter the value of x:";
        cin >> z;
    }
    if(c=='s')
    {
        stepexecution(x,y,z);
    }
    if(c=='q')
    {
        exit(0);
    }
}

}

Expr.cpp -

#include "Expr.h"

void Expr::setRece(double x)
{
rece = x;
}
void Expr::setArg(double y)
{
arg = y;
}
//Constructor.
Expr::Expr()
{

}

Expr.h -

#ifndef ____Expr__
#define ____Expr__

#include <iostream>
#include "math.h"

using namespace std;

 class Expr {
public:
void setRece(double x);
void setArg(double y);
Expr();
protected:
double arg, rece;
};

#endif /* defined(____Expr__) */

二进制.cpp -

#include "Binary.h"

double Binary::sum()
{
return rece+arg;
}

double Binary::subt()
{
return rece-arg;
}

double Binary::mult()
{
return rece*arg;
}

double Binary::divi()
{
return rece/arg;
}

double Binary::exp()
{
return pow(rece,arg);
}

二进制.h -

#ifndef ____Binary__
#define ____Binary__
#include "Expr.h"
#include <iostream>

using namespace std;

 class Binary : public Expr
 {
public:
double sum();
double subt();
double mult();
double divi();
double exp();

};
#endif

感谢您的帮助:)

【问题讨论】:

  • 你能显示make文件或者你是如何编译的吗?

标签: c++ x86-64


【解决方案1】:

此错误表示您没有正确链接目标文件。所有目标文件都必须链接到最终程序。

如果它们被链接,那么有一些函数不在任何链接的目标文件中。请链接正确的文件。

编译一个文件只是为了产生obj文件使用

  g++ -o binary.o -c binary.cpp
  g++ -o expr.o -c expr.cpp

终于与主文件链接使用,

 g++ -o main main.cpp expr.o binary.o

【讨论】:

  • nit:因为这是c++,你应该使用g++作为链接,否则一些c++库可能没有链接。
  • @SylvainDefresne:已更正。
  • 那么我需要在这里链接哪些 obj 文件?
猜你喜欢
  • 2014-05-14
  • 1970-01-01
  • 2018-07-18
  • 2015-03-01
  • 1970-01-01
  • 2018-03-11
  • 2014-04-15
  • 2017-02-15
  • 1970-01-01
相关资源
最近更新 更多