sin() and cos() in C++ function gives link error

Building my libraries on Mac suddenly started to fail with XCode Version 11.3 (11C29).

I was able to isolate one problem:


It seems that when you use sin and cos in the same function, the optimizer will use __sincosf_stret to calculate both at once but this result in a link error.


So what did I do:

  1. Create a new XCode project, cmd line tool, objectiv-c
  2. Change the main.m to main.mm to allow C++
  3. Change main as:
#import <Foundation/Foundation.h> 
#include <cmath>
void coordinateCalculator(float angle, float radius, float& x, float& y)
{
float mySin = sin(angle);
float myCos = cos(angle);
x = radius * myCos;
y = radius * mySin;
}
int main(int argc, const char * argv[])
{
@autoreleasepool
{
float myAngle = 0.0;
float theX, theY;
float radius = 100.0;
while (myAngle < 360.0)
{
coordinateCalculator(myAngle, radius, theX, theY);
NSLog(@"My coordinates: %f, %f",theX,theY);
myAngle += 1.0;
}
}
return 0;
}

I'm using in my project:

  • C++: C++14, libC++
  • Base SDK: MacOS
  • Deployment target 10.14


Building in Debug works fine, building for profiling (Release version with optimization) will fail giving the following error message:


Undefined symbols for architecture x86_64:
"___sincosf_stret", referenced from:
coordinateCalculator(float, float, float&, float&) in main.o
_main in main.o
ld: symbol(s) not found for architecture x86_64


When I change the deployment target to 10.7, the sincosf_stret is found, but I get 2 ARC errors:


Undefined symbols for architecture x86_64:
"_objc_loadClassref", referenced from:
__ARCLite__load() in libarclite_macosx.a(arclite.o)
"_objc_readClassPair", referenced from:
__ARCLite__load() in libarclite_macosx.a(arclite.o)
ld: symbol(s) not found for architecture x86_64


Trying different deployment targets give:

  • 10.7: 2 ARC link errors
  • 10.10: 2 ARC link errors AND _sincosf_ret error
  • 10.12 or higher: _sincosf_ret error


I can't belief that it is not possible to use sin/cos in one function. So I hope someone has a hint to resolve this problem. I have to build multiplatform libraries. It worked fine for years. Recently I switched to XCode 11 and to BaseSDK "MacOS". Before I used some special tool to reinstall BaseSDK 10.7 for XCode 9. I wanted to get rid of the necessety of using this special tool for reinstalling BaseSDK 10.7 after each XCode update.



Thanks,

Rudi

Accepted Reply

First I built it on 10.15.3. Then I tried 10.14.6. I ran it under the time profiler with default settings, which appear to be "fastest,smallest". I also tried running the exported archive from the command line. Again, no problems.


But isn't your complain about linking? What does it matter how I run it then?

Replies

Your code works fine for me

My OS is 10.14.6, can't upgrade to Catalina yet as I have some code not running on 64 bit, working on it. What OS do you have?


Did you try: "Build for profiling" with for profiling optimization settings "Fastes, Smallest"? In Debug mode it works for me, not with optimization enabled.


Thanks already for your testing.

Note that in your loop in main, you are taking the sin/cos of 360 radians, incrementing by 1 radian each cycle. 1 radian is little over 57.0 degrees. All trig functions in the standard library take radians, not degrees, as the measure of an angle.

Yes I know. Not that important. I just needed a function with sin and cos. This is just a stupid example project. My real files are much bigger and much more complicated.


Important is to find a way to get this linked with optimized compiliation.


Thanks anyway for letting me know.

First I built it on 10.15.3. Then I tried 10.14.6. I ran it under the time profiler with default settings, which appear to be "fastest,smallest". I also tried running the exported archive from the command line. Again, no problems.


But isn't your complain about linking? What does it matter how I run it then?

Hi John,


Thanks again for testing this on your system. Knowing that it works at your side, made me thinking. I just have deleted the XCode 11 applicaton and downloaded a new one. With this new instaal the sincos issue is gone. My library is happy building again. It must have been the tool installing former SDKs that had replaced the LLVM compiler with an older version.


I'm up and running again.


Thanks,


Rudi