I just finished up a class in C/C++ in a Linux environment and when I copied over and ran some simple codes on my Mac (OS-X 10.7 Lion), I ran into some errors. I use gcc/g++ on both platforms. It seems that the prototyping isn’t transferring properly: when I prototype the mysin.cpp file, it spits out the errors below, but when I copy the function definition in its place (i.e. put all code in one file), it works fine.
output:
J-MacBook-Pro jh$ g++ -o main main.cpp -lm
Undefined symbols for architecture x86_64:
"mysin(double, double)", referenced from:
_main in cc67Vpm6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
mysin.cpp
#include <math.h>
double mysin(double x, double tol) {
int N;
int ii = 1;
double q;
double sN = x;
double cont = x;
double term = x;
while (fabs(term) > tol) {
term = term * (-x * x) / ((2 * ii + 1) * (2 * ii));
sN = sN + term;
ii = ii + 1;
}
return sN;
}
main.cpp
#include <iostream>
#include <math.h>
double mysin(double x, double tol);
using namespace std;
int main(){
cout << "sin(1) = " << mysin(1, 1e-6) << endl;
}
It seems like the problems are with some type of name mangling, but I can’t really tell what’s going on. Any ideas? Something incredibly obvious I’m missing?
you need to compile in two stages or compile all c files in one go like
or
Otherwise g++ will look only at the main.cpp and cannot find your mysin function