体系结构x86_64的未定义符号:El Capitan

Undefined symbols for architecture x86_64: El Capitan

本文关键字:符号 El Capitan 未定义 x86 体系结构      更新时间:2023-10-16

我正在使用Mac OSX 10.11 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;
}

有人能帮我吗?

在您的代码中,您缺少Graph::BFS(int)实现,但它是在类定义中定义的:

void BFS(int s);

如果您不使用此方法(它将被优化器删除),这甚至会起作用,但是,您在代码中使用它,并且此方法没有实现。

所以这不是操作系统/编译器的错误,而是您自己的错误。更重要的是,这段代码以前甚至不能链接,所以你可能需要更改它。