使用标题编译C程序(新手)
发布时间:2020-12-16 09:42:57 所属栏目:百科 来源:网络整理
导读:我正在从Edward Schneinerman的C for Mathematicians那里学习C.我正在研究第2章中最大的公约数部分.我已经制作了三个文件: gcd.h #ifndef GCD_H#define GCD_Hlong gcd(long a,long b);#endif gcd.cc #include "gcd.h"#includeiostreamusing namespace std;l
我正在从Edward Schneinerman的C for Mathematicians那里学习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的新手,并没有完全grokked编译和链接.我究竟做错了什么? 解决方法
您应该尝试编译两个C文件:
$g++ -Wall gcdtest.cc gcd.cc -o gcdtest.exe 否则你声明了’gcd'(在你的头文件中),但是编译器缺少它的实现. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |