Jump to content
OMRON Forums

sqrt(float) compiles in Debug, not Release?


smr99
 Share

Recommended Posts

I have a C-language Background Program that uses a C-language Library.

 

There is a library function that contains the following:

float Foo(float x)
{
return sqrt(x);
}

 

Using the Delta Tau IDE, this compiles in Debug configuration. In Release configuration, however, while the library itself compiles, the background program fails to link. Oddly, the IDE claims the build succeeded and its build window doesn't show any error. But the background program's ".out" file is not created and its "err.log" file contains

undefined reference to `sqrtf'

 

What's going on?

Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

I would bet that this is an optimization issue. If you look at the definition of sqrt() in rtpmaclib.h, it accepts double arguments (not float).

 

When you compile and link in debug, an optimization flag might be getting set, the end result being that the compiler automatically casts from float to double for you. Then, if that same flag doesn't get set when you compile and link in release, the compiler won't auto-cast. When it goes to link it tries to find a sqrt() function that accepts float and when it can't find one it fails.

 

That is my guess at least. Try switching from float to double and see what happens.

Link to comment
Share on other sites

I would bet that this is an optimization issue. If you look at the definition of sqrt() in rtpmaclib.h, it accepts double arguments (not float).

 

When you compile and link in debug, an optimization flag might be getting set, the end result being that the compiler automatically casts from float to double for you. Then, if that same flag doesn't get set when you compile and link in release, the compiler won't auto-cast. When it goes to link it tries to find a sqrt() function that accepts float and when it can't find one it fails.

 

That turns out to be correct, to my surprise. As you note, the sqrt() prototype is for double, so I actually expected a compile error. And I was blaming Delta Tau. But I just tried the same code on a pure linux machine with gcc and discovered that with no optimization, the code does indeed call sqrt() -- presumably after promoting the argument to double -- but with optimization (even just -O1), it changes to call sqrtf(). So this is either a GCC optimization or Standard C. Interesting.

 

Anyway, as you guessed, an explict cast of the argument fixes the problem so I'll do that.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share


×
×
  • Create New...