"undefined reference to 'cblas_ddot'" when using cblas library
Solution 1
You can't just include the header - that only tells the compiler that the functions exist somewhere. You need to tell the linker to link against the cblas library.
Assuming you have a libcblas.a
file, you can tell GCC about it with -lcblas
.
The web site for GNU Scientific Library tells you how to do this:
Solution 2
My problem was just solved. The reason is that I made a mistake when inputed the link path. Thanks for Jonathon Reinhart's answers, they are really helpful when learning how to code in linux.
The compile commands are:
gcc -c test.c
gcc -L/usr/lib64 test.o -lgsl -lgslcblas -lm
Where "/usr/lib64" is the correct link path.
![Old Panda](https://i.stack.imgur.com/ptIHF.jpg?s=256&g=1)
Comments
-
Old Panda about 2 years
I was testing the cblas ddot, and the code I used is from the link and I fixed it as
#include <stdio.h> #include <stdlib.h> #include <cblas.h> int main() { double m[10],n[10]; int i; int result; printf("Enter the elements into first vector.\n"); for(i=0;i<10;i++) scanf("%lf",&m[i]); printf("Enter the elements into second vector.\n"); for(i=0;i<10;i++) scanf("%lf",&n[i]); result = cblas_ddot(10, m, 1, n, 1); printf("The result is %d\n",result); return 0; }
Then when I compiled it, it turned out to be:
/tmp/ccJIpqKH.o: In function `main': test.c:(.text+0xbc): undefined reference to `cblas_ddot' collect2: ld returned 1 exit status
I checked the cblas file in
/usr/include/cblas.h
, and noticed there isdouble cblas_ddot(const int N, const double *X, const int incX, const double *Y, const int incY);
I don't know where it is going wrong. Why does the compiler said the "cblas_ddot" is undefined reference?
-
Old Panda over 10 yearsI tried
gcc -Wall -I/usr/include -c test.c
and it works. But then I executedgcc test.o -o test
, it still wasundefined reference to 'cblas_ddot'
. -
Seng Cheong over 10 yearsThe first invokation of
gcc
is just compilingtest.c
totest.o
. The library is not used at all yet. If you runreadelf -s test.o
, you will see the unresolved symbolcblas_ddot
. Your second invokation ofgcc
is just linkingtest.o
againstlibc
and creating the final output executable. It is here that you need to specify linking against the cblas library. -
Old Panda over 10 yearsI got it. Then I tried the method here, there is still an error
/usr/bin/ld: cannot find -lcblas
. -
Seng Cheong over 10 yearsHave you installed the library?
-l
will look in the standard directories, which does not include the current directory. -
Seng Cheong over 10 years
find /lib* /usr/lib* -name lib*.a
-
Old Panda over 10 yearsThank you very much! It seems there is no lcblas or anything looked like it. Maybe the library hasn't been installed. Thanks again.