I’m a beginner at c++ and I’m trying to write a program to find greatest common factor. In main i have:
#include <iostream>
#include <cstdlib>
#include "longgcd.cpp"
int main(int argc, char* argv[]){
long gcd(long m, long n);
long m,n,g;
m=atol(argv[1]);
n=atol(argv[2]);
g=gcd(m,n);
std::cout<<"gcd("<<m<<","<<n<<")="<<g<<std::endl;
return 0;
}
and then i put the subfunction into another file called longgcd.cpp
#include <iostream>
#include <cstdlib>
long gcd( long m, long n){
long r;
while(n !=0){
r=m%n;
m=n;
n=r;
}
return m;
}
somehow longgcd.cpp can’t compile. i get an error:
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [longgcd] Error 1
somehow I have difficulty running this program and making it work, i can’t see whats wrong with it. Thanks for any help or suggestions.
Can you show the line you used to compile? It sounds like you tried to compile
longgcd.cppindependently as an executable, and since that file doesn’t havemain, the linker correctly complained that it couldn’t findmain.The simplest solution is to compile both files together