BLAS sdot_ function returns unexpected result

-2 c gcc fortran blas

I'm trying to generate dot product of two matrices using the below compiling command in Linux-GCC:

gcc -L / usr / lib64 / atlas -lblas code.c

这是似乎没有错误编译的代码:

#include < stdio.h>    

int main()
{
  int n,incx,incy;
  int x[] = {1,2,3};
  int y[] = {2,3,4};
  int z;
  incx =1;
  incy =1;
  n = 3;
  z = sdot_(&n, x, &incx, y, &incy);
  printf("%d", z);
  printf("\n");
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

因此,我希望看到1 * 2 + 2 * 3 + 3 * 4 = 20,但是当我运行二进制文件时,将打印结果“ 3”。

有任何想法吗?

Vla*_*r F 5

您使用了错误的类型和的隐式声明sdot_。您应该添加函数原型(或包括标题)并使用正确的类型。

#include <stdio.h>    

float sdot_(int* n, float* x, int* incx, float* y, int* incy);

int main()
{
  int n,incx,incy;
  float x[] = {1.f,2.f,3.f};
  float y[] = {2.f,3.f,4.f};
  float z;
  incx =1;
  incy =1;
  n = 3;
  z = sdot_(&n, x, &incx, y, &incy);
  printf("%f", z);
  printf("\n");
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果启用警告,则将针对您的代码版本从编译器获取警告:

cc sdot.c -lblas -Wall
sdot.c: In function ‘main’:
sdot.c:14:3: warning: implicit declaration of function ‘sdot_’ [-Wimplicit-function-declaration]
Run Code Online (Sandbox Code Playgroud)

  • 然后,您应将答案标记为已接受。 (4认同)