"undefined reference to 'cblas_ddot'" when using cblas library

14,363

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.

Share:
14,363
Old Panda
Author by

Old Panda

南村群童欺我老无力

Updated on June 12, 2022

Comments

  • Old Panda
    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 is

    double 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
    Old Panda over 10 years
    I tried gcc -Wall -I/usr/include -c test.c and it works. But then I executed gcc test.o -o test, it still was undefined reference to 'cblas_ddot'.
  • Seng Cheong
    Seng Cheong over 10 years
    The first invokation of gcc is just compiling test.c to test.o. The library is not used at all yet. If you run readelf -s test.o, you will see the unresolved symbol cblas_ddot. Your second invokation of gcc is just linking test.o against libc and creating the final output executable. It is here that you need to specify linking against the cblas library.
  • Old Panda
    Old Panda over 10 years
    I got it. Then I tried the method here, there is still an error /usr/bin/ld: cannot find -lcblas.
  • Seng Cheong
    Seng Cheong over 10 years
    Have you installed the library? -l will look in the standard directories, which does not include the current directory.
  • Seng Cheong
    Seng Cheong over 10 years
    find /lib* /usr/lib* -name lib*.a
  • Old Panda
    Old Panda over 10 years
    Thank you very much! It seems there is no lcblas or anything looked like it. Maybe the library hasn't been installed. Thanks again.