我不知道爲什麼我不能鏈接這個程序。首先這是我的頭文件,gcd.h:簡單的C++程序不能鏈接到Windows下的MingW
#ifndef GCD_H
#define GCD_H
/**
* Calculate the greatest common divisor of two integers.
* Note: gcd(0,0) will return 0 and print an error message.
* @param a the first integer
* @param b the second integer
* @return the greatest common divisor of a and b
*/
long gcd(long a, long b);
#endif
這是我gcd.cpp文件:
#include "gcd.h"
#include <iostream>
using namespace std;
long gcd(long a, long b) {
// if a and b are both zero, print an error and return 0
if ((a==0) && (b==0)) {
cerr << "WARNING: gcd called with both arguments equal to zero." << endl;
return 0;
}
// Make sure a and b are both nonnegative
if (a<0) {
a = -a;
}
if (b<0) {
b = -b;
}
// if a is zero, the answer is b
if (a==0) {
return b;
}
// otherwise, we check all the possibilities from 1 to a
long d; // d will hold the answer
for (long t=1; t<=a; t++) {
if ((a%t==0) && (b&t==0)) {
d = t;
}
}
return d;
}
的主要問題是,當我編譯,則返回錯誤
c:/ mingw/bin /../ lib/gcc/mingw32/4.5.2 /../../../ libmingw32.a(main.o):main.c :(.text + 0xd2 ): 未定義引用'WinMain @ 16'collect2:ld返回1退出 狀態
我不明白這是什麼意思。
請幫忙?
好吧其實可以有人只是修改我的代碼,以便它正常運行?這是目前最好的選擇,因爲那樣我纔會明白我做錯了什麼。
你使用什麼編譯器?我猜gcc是因爲包含在錯誤信息中的路徑 –
他/她正在使用GCC。你可以在他們的路上看到它。 – kmdent
與問題無關:有很多更有效的GCD實施方式,您應該嘗試Google以獲得更好的解決方案。 –