Reputation: 3327
I do not know why I cannot link this program. First off this is my header file, gcd.h:
#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
And this is my gcd.cpp file:
#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;
}
The main problem is when I compile, it returns the error
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../libmingw32.a(main.o):main.c:(.text+0xd2): undefined reference to `WinMain@16' collect2: ld returned 1 exit status
I don't understand what that means.
Please help?
Okay actually can someone just modify my code so it runs properly? That's the best bet at this point because then I'll actually understand what I did wrong.
Upvotes: 2
Views: 4271
Reputation: 7778
How are you compiling your code? It should be something like:
g++ gcd.cpp -o gcd
Do not #include <windows.h>
or add -mwindows
to your command.
Upvotes: 0
Reputation: 283793
This isn't a program, it's a single function.
It can be independently compiled, but it can't be linked into an executable because it has no entry point. The linker is complaining about the missing entry point (according to the name expected by your compiler's startup code).
Upvotes: 0
Reputation: 96306
where is your main
function (the entry point of the program..)?
Btw I like that you wrote "the main problem" :)
Upvotes: 5