我试着用cblas库来使用BLAS。我从自定义C函数调用cblas函数,然后将该文件链接到我的C++文件。但我知道这个错误
Cfile.o: In function `mm_blas':
Cfile.c:(.text+0xbf): undefined reference to `cblas_dgemv'
collect2: error: ld returned 1 exit status我的Cfile.c
#include <stdio.h>
#include <cblas.h>
void f(double dd[])
{
int i;
for (i=0; i<2; ++i) printf("%5.1f\n", dd[i]);
printf("\n This is a C code\n");
}
double m[] = {
3, 1, 3,
1, 5, 9,
2, 6, 5
};
double x[] = {
-1, -1, 1
};
double y[] = {
0, 0, 0
};
void mm_blas(double m1[], double m2[], double m3[], int m_size){
cblas_dgemv(CblasRowMajor, CblasNoTrans, 3, 3, 1.0, m, 3, x, 1, 0.0, y, 1);
}和main.cpp
#include <iostream>
#include <fstream>
#include <random>
#include <chrono>
#include <time.h>
#include <string>
#include <algorithm>
#include <ostream>
using namespace std;
using namespace std::chrono;
extern "C" {
#include <stdio.h>
#include <cblas.h>
void f(double []);
void mm_blas(double [], double [], double [], int);
}
void func(void)
{
std::cout<<"\n being used within C++ code\n";
}
int main(void)
{
double dd [] = {1,2,3};
f(dd);
func();
return 0;
}我用命令编译代码
gcc -c -lblas Cfile.c -o Cfile.o && g++ -std=c++11 -lblas -o myfoobar Cfile.o main.cpp && ./myfoobar我在Linux上,gcc 4.8。如何解决这个问题。
发布于 2016-06-16 18:05:01
取而代之的是这样做:
gcc -c Cfile.c -o Cfile.o && g++ -std=c++11 -o myfoobar Cfile.o main.cpp -lblas && ./myfoobar( a)将链接选项(-lblas)传递给gcc -c ...是没有意义的,因为-c的意思是不链接。
( b)在链接序列中,默认情况下,需要符号定义的文件必须发生在提供定义的文件之前。因此,对象文件在它们所引用的库之前,否则库将被忽略,而引用则未得到解决。
https://stackoverflow.com/questions/37861513
复制相似问题