ld: symbol(s) not found for architecture x86_64 (run C++ on VS code)

Viewed 630

I am creating a simple C++ project of the relationship between points and a circle in VS code (macOS Version 12.1). My code worked well if I declared and defined Point and Circle classes within the main script. However, when I declared and defined classes in .cpp and .h separately, I got the following error:

Undefined symbols for architecture x86_64:
  "Point::getX()", referenced from:
      isInCircle(Circle&, Point&) in tempCodeRunnerFile-058de6.o
  "Point::getY()", referenced from:
      isInCircle(Circle&, Point&) in tempCodeRunnerFile-058de6.o
  "Point::setX(int)", referenced from:
      _main in tempCodeRunnerFile-058de6.o
  "Point::setY(int)", referenced from:
      _main in tempCodeRunnerFile-058de6.o
  "Circle::getR()", referenced from:
      isInCircle(Circle&, Point&) in tempCodeRunnerFile-058de6.o
  "Circle::setR(int)", referenced from:
      _main in tempCodeRunnerFile-058de6.o
  "Circle::getCenter()", referenced from:
      isInCircle(Circle&, Point&) in tempCodeRunnerFile-058de6.o
  "Circle::setCenter(Point)", referenced from:
      _main in tempCodeRunnerFile-058de6.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Here is my tasks.json file:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang build active file",
            "command": "/usr/bin/clang",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${workspaceFolder}/*.cpp",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "compiler: /usr/bin/clang"
        }
    ]
}

My previous compiling without self-defined headers was all good. But I don't see any mistakes in my code, so I guess it's a setting or system issue. Appreciate your kind help!


This is my Point.hfile:

#pragma once 
#include <iostream>
using namespace std;

class Point
{
private:
    int m_X, m_Y;
public:
    void setX(int x);
    int getX();
    void setY(int y);
    int getY();
};

This is Point.cppfile:

#include "Point.h"
void Point::setX(int x){ m_X = x; } 
int Point::getX(){ return m_X; };
void Point::setY(int y){ m_Y = y; }
int Point::getY(){ return m_Y; };

This is Circle.hfile:

#pragma once
#include <iostream>
#include "Point.h"
using namespace std;

class Circle
{
private:
    int m_R;
    Point m_Center;   
public:
    void setR(int r);
    int getR();
    void setCenter(Point center); 
    Point getCenter(); 
};

This is Circle.cppfile:

#include "Circle.h"
void Circle::setR(int r){ m_R = r; }
int Circle::getR(){ return m_R; };
void Circle::setCenter(Point center){ m_Center = center; }  
Point Circle::getCenter() { return m_Center; };   

Here is my main function:

#include "Point.h"
#include "Circle.h"
using namespace std;

void isInCircle(Circle &c, Point &p)
{
    int distance = 
    (c.getCenter().getX() - p.getX())*(c.getCenter().getX() - p.getX())
     + (c.getCenter().getY() - p.getY())*(c.getCenter().getY() - p.getY());
    int rDistance = c.getR() * c.getR();
    if (distance == rDistance) { cout<<"Point is on the circle."<<endl;}
    else if (distance > rDistance) { cout<<"Point is off the circle."<<endl;}
    else { cout<<"Point is in the circle."<<endl;}
}

int main(){
    Circle circle;
    circle.setR(10);
    Point center;
    center.setX(10);
    center.setY(0);
    circle.setCenter(center);

    Point myPoint;
    myPoint.setX(10);
    myPoint.setY(11);
    isInCircle(circle, myPoint);

    return 0;
}
0 Answers
Related