2016-01-09 117 views
0

我正在使用Mac OSX 10.11 El Capitan体系结构x86_64的未定义符号:El Capitan

此前我使用的是OSX 10.10。我的旧版OSX我运行的是gcc 4.9g++ 4.9。但升级到OSX 10.11后,所有C++程序开始无法编译。

然后我在OSX 10.11切换回gcc 4.2和我收到以下错误:

Undefined symbols for architecture x86_64: 
    "Graph::BFS(int)", referenced from: 
     _main in BFS-e06012.o 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

我尝试了所有的答案。我试过这些命令来运行:

$ g++ -stdlib=libstdc++ BFS.cc -o BFS 
$ g++ -lstdc++ BFS.cc -o BFS 
$ gcc -lstdc++ BFS.cc -o BFS 
$ g++ BFS.cc 

但是没有什么适合我的。

当我在外壳上启动gcc --version。我得到这个:

gcc --version 
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++/4.2.1 
Apple LLVM version 7.0.2 (clang-700.1.81) 
Target: x86_64-apple-darwin15.2.0 
Thread model: posix 

我试图运行是BFS.cc这是继程序:

/* 
* Algo: BFS 
*/ 
#include <iostream> 
#include <list> 

using namespace std; 

class Graph { 
    int V; 
    list<int> *adj; 

    public: 
     Graph(int V); 
     void addEdge(int v, int w); 
     void BFS(int s); 
}; 

Graph::Graph(int V) { 
    this->V = V; 
    adj = new list<int> [V]; 
} 

void Graph::addEdge(int v, int w) { 
    adj[v].push_back(w); 
} 

int main(int argc, char const *argv[]) { 
    Graph g(4); 
    g.addEdge(0, 1); 
    g.addEdge(0, 2); 
    g.addEdge(1, 2); 
    g.addEdge(2, 0); 
    g.addEdge(2, 3); 
    g.addEdge(3, 3); 

    cout << "Following is Breadth First Traversal (starting from vertex 2) \n"; 
    g.BFS(2); 
    return 0; 
} 

谁能帮助我在这?

回答

1

在你的代码已经丢失Graph::BFS(int)实现但它是在类定义中定义:

void BFS(int s); 

,如果你不会使用这种方法(它会通过优化被删除),这甚至会工作,但是,你在你的代码中使用它,这个方法没有实现。

所以这不是一个OS /编译器故障,但只有你自己的。甚至更多 - 此代码甚至无法链接,因此您可能需要更改它。

+0

实现就在那里。我错过了复制它。 –

+0

然后这是另一个问题。你必须检查你发布的内容,现在完全由你来修改这个问题。 –

相关问题