Link to Fortran library (Lapack) from C ++

I use Lapack in my C ++ code. I'm rather confused about how to properly contact the library. Here is a small example corresponding to my code calling a function from Lapack:

#include <iostream>

namespace lapack { extern "C" {
  void ilaver(int* major, int* minor, int* patch); } }

int main()
{
    int major = 0;
    int minor = 0;
    int patch = 0;
    lapack::ilaver(&major, &minor, &patch);
    std::cout << major << "." << minor << "." << patch << std::endl;
    return 0;
}

If I try to compile it using GCC 4.8.5 (Linux openSUSE), I get the following error:

> g++ ilaver.cpp -o ilaver -L /softs/lapack/3.7.1/64/gcc/4.8.5/lib64 -l lapack
/tmp/ccHvDCAh.o: In function `main':
ilaver.cpp:(.text+0x33): undefined reference to `ilaver'
collect2: error: ld returned 1 exit status

I realized that this is a problem with the name change. If I change my code by adding an underscore at the end of the function name, it compiles correctly with GCC:

#include <iostream>

namespace lapack { extern "C" {
  void ilaver_(int* major, int* minor, int* patch); } }

int main()
{
    int major = 0;
    int minor = 0;
    int patch = 0;
    lapack::ilaver_(&major, &minor, &patch);
    std::cout << major << "." << minor << "." << patch << std::endl;
    return 0;
}

But it does not compile with Intel compilers for Windows. There, mangling is different, I have to change it to lapack::ILAVER, and then it compiles.

(Linux/Mac/Windows) (GCC, Intel, MSVC). , ?

+4
1

, , , . "" .

- LAPACKE, C LAPACK. , .

:

#include <iostream>
#include <lapacke.h>    

int main()
{
    // By using lapack_int, we also support LAPACK-ILP64
    lapack_int major = 0;
    lapack_int minor = 0;
    lapack_int patch = 0;
    LAPACKE_ilaver(&major, &minor, &patch);
    std::cout << major << "." << minor << "." << patch << std::endl;
    return 0;
}

.

, LAPACKE LAPACK, BLAS, CBLAS.

+2

Source: https://habr.com/ru/post/1685457/


All Articles