Error while trying to link two different math libraries with same symbols

100 views Asked by At

While linking the cmath and AMD math library to the C++, observing that the AMD math library replaces all the functions from cmath.

While debugging using GDB, found the following output which testifies the above statement.

commandline output from Gdb: commandline output from Gdb

Attaching the code snippet which is used to check above statement.

#include "amdlibm.h"
#include "math.h"

namespace amdfunc {
    void sqrttest() {
        printf("Calling amd func\n");
        double val = 22.34;
        double val1;
        double val2;
        for (int i = 0; i < 5000000000; i++) {
            val1 = amd_sqrt(val);
        }
    }
}

namespace normalfunc {
    void sqrttest() {
        printf("Calling cmath func\n");
        double val = 22.34;
        double val1;
        double val2;
        for (int i = 0; i < 5000000000; i++) {
            val1 = sqrt(val);
        }
    }
}

int main() {
    normalfunc::sqrttest();
    amdfunc::sqrttest();
}

Command line: Used the following command to link the libraries and compile the code.

g++ test.cpp -g -o testinglib -L/usr/lib/x86_64-linux-gnu -L/usr/lib -I/usr/include -I/home/test/include -Wl,-rpath,/home/test/lib /home/test/lib/libalm.so -lm

We reordered the libraries while compiling, observed the same error. Attaching the AMD math library link from which we used the files https://www.amd.com/en/developer/aocl/libm.html

Compiling and running the executable

2

There are 2 answers

3
Hladu On

This is intended behaviour

AOCL-LibM is a C library you can link to your applications to replace the compiler provided math functions.

Documentation page 67

https://www.amd.com/content/dam/amd/en/documents/pdfs/developer/aocl/aocl-v4.0-ga-user-guide.pdf

0
Sai Krishna Sirimisetty On

Modified the linker option with -Wl,-lm,-lalm, using

-Wl

specifically it linked both libraries and while comparing observed that both libraries are getting used.

enter image description here