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

时间:2015-06-05 17:26:39

标签: c++ ifstream lapack

我有一段非常简单的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的工作代码中使用它。由于版权原因,我无法发布sylapack.cc,但它来自此处的文档

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

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

  

&LT;目录   英特尔®MathKernel Library LAPACK示例

位于网页顶部。

我可以将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$ 

1 个答案:

答案 0 :(得分:1)

混合使用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 );
}

删除了错误。