2

I'm new to C++, so maybe I'm making a simple mistake and did not realize. I'm using XCode.

I have the simplest class ever. In my main, I create an instance of it. Fine. But when I call any method from this object, XCode throws it:

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

My code:

//A.h:
class A {
public:
    int getOne();
};

//A.cpp:
#include "A.h"
int A::getOne() {
    return 1;
}

//main.cpp:
#include <iostream>
#include "A.h"
int main(int argc, const char * argv[]) {
    A a;          //create object of class A     
    a.getOne();   //call a method
    return 0;
}
Tas
  • 7,023
  • 3
  • 36
  • 51
ElCapitan
  • 21
  • 2

1 Answers1

0

click on your project in the left frame inside of Xcode.

you will have a file structure loaded, rather, you should have a file structure loaded that will include your implementation files(.h), specification files (.cpp), and your main file (.cpp)

i remember having these clang error: linking failures when i first started using Xcode, especially when i started learning how to create classes and what not. based on that experience and my solution to them, i am almost positive that what you don't have is the files under the appropriate folder structure and linked to the project in Xcode.

to check what you have linked in Xcode you should click on the project in the left frame that has the blue Xcode icon. it will take you to a screen that has a few tabs across the top in the main frame. click on the tab that says "Build Phases". using the term tab is a little misleading, its just 4 columns of words that links to a new page inside of the main Xcode frame.

under build phases, click on the arrow that opens the file tree below "compile sources".

if you do not see your specification file and your implementation file in here then i am quite certain that your linking error is because of this minor oversight...

let me know if that helps.

edit:

inside of compile sources, just click on the + sign to add the files you need for your project

lopezdp
  • 1,538
  • 3
  • 21
  • 38