Search code examples
c++static-linking

How to know which 'sin' function does my program invoke when running?


I am using different versions of libm.a. One that I am playing with is fdlibm's libm.a (from Sun).

The problem is that I feel that my program does not call the functions in fdlibm's libm.a, but calls those of the system's glibc's libm.a.

#include "fdlibm.h"
int main(){
  double x = sin(3);
}

The program is compiled C++ programs(because it has to be linked with other c++ programs):

g++ prog.cpp libm.a

where libm.a is the fdlibm's. (From Sun, http://www.netlib.org/fdlibm/readme)

Question 1

How can I know what does sin actually invoke at run-time? I heard about various tools like objdump, gdb... Which one can be used for my case and how?

Question 2

How can I enforce fdlibm's libm.a be used?

Thanks.


Solution

  • Question 1. I heard about various tools like objdump, gdb.

    As with gdb. Create file trace_sin.gdb

    $ cat trace_sin.gdb
    set confirm off
    b sin
    commands
    bt
    c
    end
    r
    quit
    

    And run your program:

    $ gdb -q -x trace_sin.gdb   ./a.out Reading symbols from ./a.out...(no
    debugging symbols found)...done. Breakpoint 1 at 0x400498
    
    Breakpoint 1, 0x000000314941c760 in sin () from /lib64/libm.so.6
    #0  0x000000314941c760 in sin () from /lib64/libm.so.6
    #1  0x0000000000400629 in main ()
    

    As you see in my case sin comes from libm

    Question 2. How can I enforce fdlibm's libm.a be used?

    Just make sure than sin from fdlibm comes before libm's sin