gpt4 book ai didi

c++ - gcc 4.7.4 链接问题 : Undefined Symbols for Architecture x86_64

转载 作者:太空宇宙 更新时间:2023-11-04 13:00:10 24 4
gpt4 key购买 nike

我是新来的,所以我希望你能帮助我。我在一个 C++ 项目中工作,试图用终端 (Mac Os) 编译它,但是当我包含我的类的 .hpp 时我总是得到一个错误,但是当我包含 .cpp 时却没有。

I tried to make the simplest example in the main to see more clearly the issue.

这些是我的文件:

main.cpp

#include "Point.hpp"

int main(){
Point p1;
}

点.hpp

#ifndef Point_hpp
#define Point_hpp

#include <math.h>
#include <iostream>
#include <stdio.h>

class Point{
private:
double dX;
double dY;

public:
Point();
Point(double dX, double dY);

void setX(double dX);
double getX();
void setY(double dY);
double getY();

double getDistance(Point p);

void move(double dDespX, double dDespY);

void display();
};

#endif

点.cpp

#include "Point.hpp"

Point::Point(){
dX = 0;
dY = 0;
}

Point::Point(double dX, double dY){
this -> dX = dX;
this -> dY = dY;
}

void Point::setX(double dX){
this -> dX = dX;
}

double Point::getX(){
return dX;
}

void Point::setY(double dY){
this -> dY = dY;
}

double Point::getY(){
return dY;
}

double Point::getDistance(Point p){
double dDist;
dDist = sqrt(pow(p.dX - dX, 2) + pow(p.dY - dY, 2));
return dDist;
}

void Point::move(double dDespX, double dDespY){
dX += dDespX;
dY += dDespY;
}

void Point::display(){
std::cout << "Point = (" << dX << "," << dY <<")" << std::endl;
}

当我尝试编译(使用'make main')时,我从终端得到这个:

$ make main
c++ main.cpp -o main
Undefined symbols for architecture x86_64:
"Point::Point()", referenced from:
_main in ccVzh5gg.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make: *** [main] Error 1

最佳答案

您需要将Point.cpp 添加到您在makefile 中编译和链接的文件列表中。所以将编译行更改为

c++     main.cpp  Point.cpp  -o main

因为您没有将 Point.cpp 文件中的定义链接到可执行文件,所以您无权访问 Point.cpp 中提供的函数,您只能在编译时知道它们的声明(参数和返回值)。

关于c++ - gcc 4.7.4 链接问题 : Undefined Symbols for Architecture x86_64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44476172/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com