2013-10-29 32 views
0

我使用了g ++編譯器,並使用終端編譯單個C++文件和項目(在項目中我指的是同一目錄中的文件,但不是真正的Xcode項目)。 我沒有問題,但我升級到OS X Mavericks,從那時起,我只能編譯單個文件。之後,我安裝了Xcode 5.0.1,並安裝了命令行工具,但沒有解決我的問題。通過g ++在小牛終端上編譯時出錯

因此,現在我正在使用 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; 
} 

預先感謝您!

+0

你說「之後,我安裝了Xcode 5.0.1,並安裝了命令行工具,但沒有解決我的問題」,但Xcode不包含'gcc',所以你必須使用'clang',但是你不會這麼說,這讓我相信你繼續使用'g ++'...... – trojanfoe

+0

@trojanfoe Xcode確實包含了GCC,這不是問題所在。 – 2013-10-29 11:04:27

+2

@OP:錯誤是完全明顯的,你不編譯/鏈接'MyClass.cpp'文件。 (Honnantudnászerencsétlengcc,hogy melyikfájlokatfordítsa,ha nem adod meg neki !?) – 2013-10-29 11:05:13

回答

1

除非我被厚(暗示,我不是),你還沒有實現

MyClass::geta()。 相反,你實現了一個名爲geta

功能你的類中的方法應該是這樣的:

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

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

此外,你不能被編譯MyClass.cpp,因爲代碼中存在無效。