在Mavericks上通过终端上的g ++进行编译时出错

时间:2013-10-29 11:00:39

标签: c++ gcc

我使用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)

项目的文件: main.cpp中

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



int main(){

    Myclass one;
    cout<<one.geta()<<endl<<one.getb()<<endl;

    return 0;
}

Myclass.h

#include <iostream>

class Myclass
{
private:
    int a;
    double b;
public:
    Myclass();
    int geta();
    double getb();
};

Myclass.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;
}

提前谢谢!

1 个答案:

答案 0 :(得分:1)

除非我很厚(暗示,我不是),否则你没有实施

MyClass::geta()。 相反,您实现了一个名为geta

的函数

你的班级方法应该是:

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

int MyClass::geta()
{
    ....
}

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

相关问题