c++cvisual-studio-2010c-libraries

Using FDLIBM library in Visual Studio, C++


I'm porting some code from MATLAB to C++ and discovered that MATLAB's sin() and cos() functions produce slightly different results from the sin() and cos() functions in the C++ library. To eliminate these differences, I would like my C++ code to call the sin() and cos() functions from the fdlibm 5.3 library, which is what I think MATLAB uses for sin() and cos() operations.

However, I have been having some difficulty using the fdlibm library. I am using Visual Studio 2010, and downloaded the fdlibm header file and source codes from http://www.validlab.com/software/, but am not sure the best way to use these files. Do I need to first build the files into a static or dynamic library, and then link it to my code? Also, how do I specify that I want to use the sin() from fdlibm, rather than from C++ library? Do I need to modify the fdlibm source code so that the sin() and cos() functions are within a namespace?

Any guidance is greatly appreciated.


Solution

  • Essentially, you have two tasks to complete:

    I see two issues with the first task. One, sources from projects like fdlibm are typically written to be portable to many systems and may involve a fair amount of work to configure. Rather than being very simple C or C++ code, they may use a number of preprocessor conditionals to select certain options, and the package the sources come in may have scripts to make various preparations for compiling.

    Two, you want the sources to match the C++ standard’s specification for declaring sin and cos. If the fdlibm package you have supports C++, this might not require any work on your part. Otherwise, you may have to modify the sources to wrap the sin and cos definitions inside the std namespace, or otherwise modify the sources.

    The second issue is linking. Using a library is not required. You can simply compile the source file(s) containing sin and cos to produce an object module (or modules), then link that object module (or modules) with your other object modules. If you wish, you can instead create a library, put the object module(s) with sin and cos into the library, and link the library with your object modules. With most common linkers, you can link a library with your object modules simply by listing it as input the linker, the same way object modules are listed. (Some linkers also have other options for referring to libraries, but simply giving its normal file path is usually sufficient.) You can create and link either a static or a dynamic library, as you prefer. If you use a dynamic library, it must be present when the executable runs. For a simple application for your own use, there is no need to use a dynamic library (or even to use a static library; object modules are fine). (Essentially, the purpose of libraries is to make distributing object modules to other people easier, or to organize large projects. Simple applications do not need libraries.)

    Another note about linking: When you supply your own sin and cos, the linker has two implementations to choose from: Your implementations of sin and cos and the standard library implementations of sin and cos. Usually, standard libraries are linked in after any user-specified files, so merely specifying your object module or library will suffice to ensure your sin and cos are used, not the library’s sin and cos. In the event this is not the case, there should be linker options to change the order in which libraries are considered.