如何将 C++ 代码集成到工作的 LAPACK 代码中

How to integrate c++ code into working LAPACK code

我有一段非常简单的 C++ 代码 justread.cc 从文件中读取数字 。命令后

ludi@ludi-M17xR4:~/Desktop/tests$ g++ -Wall -pedantic -o justread.x justread.cc && ./justread.x

它编译时没有任何错误或警告。这是代码:

#include <fstream>
#include <vector>


int read_covariance ()
  {
    std::vector<double> data;
    double tmp;

    std::ifstream fin("peano_covariance.data");

    while(fin >> tmp)
    {
        data.push_back(tmp);
    }

    return 0;
}

int main()
{
read_covariance();
return 0;
}

我希望在另一个名为 sylapack.cc. 的工作代码中使用它,由于版权原因,我不能 post sylapack.cc,但它取自此处的文档

https://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/dsyev_ex.c.htm

除了删除伪线外没有任何修改

< Table Of Contents Intel® Math Kernel Library LAPACK Examples

在网页顶部。

我可以将 sylapack.cc 编译为 C 代码,没有错误或警告:

ludi@ludi-M17xR4:~/Desktop/tests$ gcc -Ddsyev=dsyev_ -o sylapack sylapack.c -L/usr/local/lib -llapack -lblas && ./sylapack
 DSYEV Example Program Results

 Eigenvalues
 -11.07  -6.23   0.86   8.87  16.09

 Eigenvectors (stored columnwise)
  -0.30  -0.61   0.40  -0.37   0.49
  -0.51  -0.29  -0.41  -0.36  -0.61
  -0.08  -0.38  -0.66   0.50   0.40
  -0.00  -0.45   0.46   0.62  -0.46
  -0.80   0.45   0.17   0.31   0.16
ludi@ludi-M17xR4:~/Desktop/tests$ 

我希望将 C++ 函数 read_covariance 集成到 sylapack.cc 中。为此,我必须用 g++ 编译 sylapack.cc。这给我错误。

ludi@ludi-M17xR4:~/Desktop/tests$ g++ -Ddsyev=dsyev_ -o sylapack sylapack.c -L/usr/local/lib -llapack -lblas && ./sylapack
sylapack.c: In function ‘int main()’:
sylapack.c:89:74: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
         dsyev( "Vectors", "Upper", &n, a, &lda, w, &wkopt, &lwork, &info );
                                                                          ^
sylapack.c:89:74: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
sylapack.c:93:72: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
         dsyev( "Vectors", "Upper", &n, a, &lda, w, work, &lwork, &info );
                                                                        ^
sylapack.c:93:72: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
sylapack.c:100:49: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
         print_matrix( "Eigenvalues", 1, n, w, 1 );
                                                 ^
sylapack.c:102:72: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
         print_matrix( "Eigenvectors (stored columnwise)", n, n, a, lda );
                                                                        ^
/tmp/ccz2hTYK.o: In function `main':
sylapack.c:(.text+0xa3): undefined reference to `dsyev_(char*, char*, int*, double*, int*, double*, double*, int*, int*)'
sylapack.c:(.text+0x12a): undefined reference to `dsyev_(char*, char*, int*, double*, int*, double*, double*, int*, int*)'
collect2: error: ld returned 1 exit status
ludi@ludi-M17xR4:~/Desktop/tests$ 

您想在混合 C++ 和 C 时有 "C" 链接。请参阅那里的解释 In C++ source, what is the effect of extern "C"?

确实在修改行

/* DSYEV prototype */
extern void dsyev( char* jobz, char* uplo, int* n, double* a, int* lda,
                double* w, double* work, int* lwork, int* info );
/* Auxiliary routines prototypes */
extern void print_matrix( char* desc, int m, int n, double* a, int lda );

这些:

/* DSYEV prototype */
extern "C"{
void dsyev( char* jobz, char* uplo, int* n, double* a, int* lda,
                double* w, double* work, int* lwork, int* info );
}
/* Auxiliary routines prototypes */
extern "C"{ 
void print_matrix( char* desc, int m, int n, double* a, int lda );
}

删除了错误。