user1089438 user1089438 - 1 year ago 94
Linux Question

Header file compatibility between Ubuntu and OS-X

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.


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


#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;



#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?

Answer Source

you need to compile in two stages or compile all c files in one go like

g++ -o main.o -c main.cpp
g++ -o mysin.o -c mysin.cpp
g++ -o main main.o mysin.o -lm


g++ -o main mysin.cpp main.cpp -lm

Otherwise g++ will look only at the main.cpp and cannot find your mysin function

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download