在小牛队的终端上通过 g++ 编译时出错

Error when compiling via g++ on terminal on Mavericks

本文关键字:g++ 编译 出错 终端      更新时间:2023-10-16

我使用了 g++ 编译器,并使用终端来编译单个 c++ 文件和项目(在项目下,我的意思是同一目录中的文件,但不是真正的 Xcode 项目)。我没有问题,但我升级到OS X Mavericks,从那时起,我只能编译单个文件。之后,我安装了Xcode 5.0.1,并安装了命令行工具,但没有解决我的问题。

所以现在我正在使用OS X 10.9 小牛Xcode 5.0.1 (5A2053)。

我想,问题出在我的源代码上,但现在我做了一个非常简单的程序,但我得到同样的错误:

Steve-MacBook:test szaboistvan$ g++ -o main main.cpp
Undefined symbols for architecture x86_64:
  "Myclass::geta()", referenced from:
      _main in main-0bDtiC.o
  "Myclass::getb()", referenced from:
      _main in main-0bDtiC.o
  "Myclass::Myclass()", referenced from:
      _main in main-0bDtiC.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

项目文件:主.cpp

#include <iostream>
#include "Myclass.h"
using namespace std;

int main(){
    Myclass one;
    cout<<one.geta()<<endl<<one.getb()<<endl;
    return 0;
}

我的班级

#include <iostream>
class Myclass
{
private:
    int a;
    double b;
public:
    Myclass();
    int geta();
    double getb();
};

我的班级.cpp

#include <iostream>
#include "Myclass.h"
using namespace std;
Myclass(){
    int a=5;
    double b=3.0;
}
int geta(){
    return a;
}
double getb(){
    return b;
}

提前谢谢你!

除非我很厚(提示,我不是),否则你还没有实现

MyClass::geta() .相反,您实现了一个名为 geta 的函数

您的类方法应如下所示:

MyClass::Myclass()
{
    int a=5;
    double b=3.0;
}
int MyClass::geta()
{
    ....
}

此外,您不能编译MyClass.cpp,因为其中的代码无效。