我正在从 Edward Schneinerman 为数学家编写的 C++ 中学习 C++。我正在研究第 2 章中的最大公约数部分。我制作了三个文件:
gcd.h
#ifndef GCD_H
#define GCD_H
long gcd(long a, long b);
#endif
gcd.cc
#include "gcd.h"
#include<iostream>
using namespace std;
long gcd(long a, long b) {
if( a == 0 && b == 0 ) {
cerr << "WARNING: gcd(0,0) not defined" << endl;
return 0;
}
if( a < 0 ) a = -a;
if( b < 0 ) b = -b;
if( b == 0 ) retrun a;
if( a == 0 ) return b;
long c = a % b;
return gcd(b,c);
}
和 gcdtest.cc
#include "gcd.h"
#include <iostream>
using namespace std;
int main() {
long a,b;
cout << "Enter the first number > ";
cin >> a;
cout << "Enter the second number > ";
cin >> b;
long d = gcd(a,b);
cout << "The gcd of " << a << " and " << b << " is " << d << endl;
return 0;
}
所有这些文件都位于同一目录中。当我尝试编译 gcdtest.cc
$ g++ -Wall gcdtest.cc -o gcdtest.exe
我收到以下错误:
$ g++ -Wall gcdtest.cc -o gcdtest.exe
Undefined symbols for architecture x86_64:
"gcd(long, long)", referenced from:
_main in ccgbCyQO.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
我是 C++ 的新手,还没有完全理解编译和链接。我究竟做错了什么?