views:

193

answers:

2

I'm in the process of porting a large'ish (~1M LOC) project from a Window/Visual Studio environment to other platforms, the first of which happens to be Mac OS X.

Originally the project was configured as Visual Studio solutions and projects, but now I'm using (the excellent) Premake (http://industriousone.com/premake) to generate project files for multiple platforms (VS, XCode, GMake).

I configured, ported and built the first few projects without any significant problems, but having ported the math lib, I ran into this weird linking error that I haven't been able to resolve: Any functions used from math.h will fail to link (causing unresolved symbols).

For reference, I'm using Premake v4.2.1 to generate projects for XCode v3.2.1, which is building using gcc v4.2 for the x86_64 architecture. (All this on 64-bit Snow Leopard) I've tried to persuade gcc to link and build everything against a 'known' SDK by adding -isysroot /Developer/SDKs/MacOSX10.6.sdk -mmacosx-version-min=10.6 to the build command line.

Now under normal circumstances, adding -lm should take care of this, however in Darwin, those math libs are included in libSystem, which, as far as I can tell, gets implicitly linked by gcc/ld.

I've tried creating a dummy project from within XCode which just runs:

float f = log2(2.0)+log2f(3.f)+log1p(1.1)+log1pf(1.2f)+sin(8.0);
std::cout << f << std::endl;

and as expected, this builds just fine. However, if I put the same thing in the code inside the Premake generated project, all those math functions end up unresolved.

Now comparing the linking command from the 'native' XCode project with my generated XCode project, they seem pretty identical (except that my generated project links other libs as well).

'Native' project:

/Developer/usr/bin/g++-4.2 -arch x86_64 -dynamiclib -isysroot /Developer/SDKs/MacOSX10.6.sdk -Lsomepath -Fsomepath -filelist somefile -install_name somename -mmacosx-version-min=10.6 -single_module -compatibility_version 1 -current_version 1 -o somename

Generated project:

/Developer/usr/bin/g++-4.2 -arch x86_64 -dynamiclib -Lsomepath -Fsomepath -filelist somefile -install_name somename -isysroot /Developer/SDKs/MacOSX10.6.sdk -mmacosx-version-min=10.6 somelib.a somelib2.a somelib.dylib somelib2.dylib -single_module -compatibility_version 1 -current_version 1 -o somename

Any help or hints about how to proceed would be most appreciated. Are there any gcc flags or other tools that can help me resolve this?

A: 

Use -lm on the command line to link with the math library.

Bertrand Marron
libm is part of libSystem on Darwin systems and specifying -lm has no effect (in fact, libm.dylib is linked to libSystem.dylib). Either way, since my test project does not explicitly specify neither libm nor libSystem, I can only assume the latter gets implicitly linked. (or that XCode somehow isn't showing me the full truth)
tbone
A: 

I finally managed to resolve/work-around this.

By replacing

#include <math.h>
float f = sinf(1.f);

with

#include <cmath>
float f = std::sin(1.f);

everything links as expected.

I'll accept the fact that the cmath solution is probably the code I should have written in the first place, although I'd happily accept further opinions about why my C approach failed so miserably.

tbone