Reputation: 845
I'd like to use math.h
in C++ Builder 10.1.2.
Unfortunately, there is a linker error when I try to call one of math.h
's functions.
What I do already know is, that (for historical reasons) the linker must be explicitly set to link to use the math lib.
See here.
In gcc this can be done via the -lm
flag.
But what do I have to enter for C++ Builder in the Project options => C++ Linker =>Advanced options field to make this work?
EDIT:
So here is an example: Create a new VCL project and change the Form1 code like that:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include <math.h>
//-------------------------------------------------------------------------- -
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
float a = floorf(0.0f);
}
//---------------------------------------------------------------------------
That will give you the linker error
[ilink32 Error] Error: Unresolved external '_floorf' referenced by C:\USERS\FLKO\DOCUMENTS\EMBARCADERO\STUDIO\PROJECTS\WIN32\DEBUG\UNIT1.OBJ
So I need to tell the linker to link with the math
lib.
But how?
Upvotes: 1
Views: 1431
Reputation: 90
This problem is not related to static/dynamic linkage but with name mangling. If you inspect math.h closely, you'll find the difference between Win64 and Win32 declarations for floor function:
extern "C++" {
...
#if defined(_WIN64)
inline float floor(float __x) { return floorf(__x); }
...
#else
...
inline float floor(float __x) { return (float)floor((double)__x); }
...
To make a long story short, try to build you project on Win64 platform: floorf will be linked correctly. It will not work for Win32. My suggestion is to use floor instead of floorf. It will work on both platforms.
Edit: Actually, floorf function is not defined for Win32 in Embarcadero std library. If you want to use some 3rd party std library, you must include appropriate header (not one from Embarcadero) and link library (#pragma link) statically. If library is compiled with gcc/msvc you must convert library to omf format.
Upvotes: 1