0

I am using Mac OSX 10.11 El Capitan.

Previously I was using OSX 10.10. I my older version of OSX I was running gcc 4.9 and g++ 4.9. But after upgrading to OSX 10.11 all C++ programs starts failing to compile.

Then I switched back to gcc 4.2 in OSX 10.11 and I am getting following error:

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)

I tried all the answers available. I tried these commands to run:

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

But nothing works for me.

When I fire gcc --version on shell. I got this:

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

The program which I was trying to run is BFS.cc which is following:

/*
* 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;
}

Can anyone help me on this?

Rajat Gupta
  • 1,909
  • 2
  • 18
  • 30

1 Answers1

1

In your code you have missing Graph::BFS(int) implementation however it is defined in the class definition:

void BFS(int s);

This would even work if you would not use this method (it will be deleted by optimizer), however, you use it in your code and this method has no implementation.

So this is not a OS/compiler fault but only your own. Even more - this code could not even link before so you might have change it.

VP.
  • 15,509
  • 17
  • 91
  • 161
  • Implementation is there. I missed to copy it. – Rajat Gupta Jan 10 '16 at 10:49
  • Then it is another question. You have to check twice what you are posting, now is it all up to you to reopen this question with making a correction. – VP. Jan 10 '16 at 10:50