赵楚涵
赵楚涵

Reputation: 13

Undefined symbols building in C++ on macOS

While implementing class member functions in.cpp file on macOS 10.15, displaying an error:

clang: Undefined symbols for architecture x86_64:error:

"Circle::Area()", referenced from:
      _main in main-4cfa92.o

"Circle::Circle(double)", referenced from:
      _main in main-4cfa92.o

linker command failed with exit code 1 (use -v to see invocation)

To verrify this case, I found an example on the website to try, and it worked fine when I copied the function definitions from Circle.cpp file into Circle.h file, But when the function declaration is in Circle.h and function definition is in Circle.cpp files, respectively, an error occurs

//Circle.h

#ifndef CIRCLE_H
#define CIRCLE_H

class Circle
{
private:
    double r;//radius
public:
    Circle();//constructor
    Circle(double R);//The constructor
    double Area();//computing area
};

#endif
//Circle.cpp

#include "Circle.h"

Circle::Circle(){
    this->r=5.0;
}
Circle::Circle(double R){
    this->r=R;
}
double Circle::Area(){
    return 3.14*r*r;
}
//main.cpp

#include <iostream>
#include "Circle.h"

using namespace std;

int main(){
    Circle c(3);
    cout<<"Area="<<c.Area()<<endl;
    return 0;
}

Error message:

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

Upvotes: 1

Views: 1065

Answers (1)

Sagar Sakre
Sagar Sakre

Reputation: 2426

Looks like you have not included Circle.cpp in the compilation step. Make sure you include both main.cpp and Circle.cpp

Upvotes: 1

Related Questions