我一直在尝试使OpenBLAS(和其他BLAS库)工作很长时间,尽管我无法使项目正确编译,但是无论我使用哪种BLAS库,它始终会产生相同类型的错误。 。
我尝试运行的代码非常简单,它是OpenBLAS git存储库中的一些示例代码。
#include "cblas.h"
#include <cstdio>
int main()
{
int i=0;
double A[6] = {1.0,2.0,1.0,-3.0,4.0,-1.0};
double B[6] = {1.0,2.0,1.0,-3.0,4.0,-1.0};
double C[9] = {.5,.5,.5,.5,.5,.5,.5,.5,.5};
cblas_dgemm(CblasColMajor, CblasNoTrans, CblasTrans,3,3,2,1,A, 3, B, 3,2,C,3);
for(i=0; i<9; i++)
printf("%lf ", C[i]);
printf("\n");
return 0;
}
在
CMakeLists.txt
文件中,我添加了以下行,以尝试将OpenBLAS库链接到我的代码:include_directories(/Path/To/OpenBlas-0.3.7)
但是,当我尝试运行代码时,将给出结果错误:
使用OpenBLAS时:
Scanning dependencies of target OpenBlasTesting
[ 50%] Building CXX object CMakeFiles/OpenBlasTesting.dir/main.cpp.o
[100%] Linking CXX executable OpenBlasTesting
Undefined symbols for architecture x86_64:
"_cblas_dgemm", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
gmake[3]: *** [CMakeFiles/OpenBlasTesting.dir/build.make:84: OpenBlasTesting] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:76: CMakeFiles/OpenBlasTesting.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/OpenBlasTesting.dir/rule] Error 2
gmake: *** [Makefile:118: OpenBlasTesting] Error 2
当使用Intel MKL,dgemm_example.c文件以及
CMakeLists.txt
文件中稍作改动的行时:Scanning dependencies of target MKLTesting
[ 50%] Building C object CMakeFiles/MKLTesting.dir/main.c.o
[100%] Linking C executable MKLTesting
Undefined symbols for architecture x86_64:
"_MKL_free", referenced from:
_main in main.c.o
"_MKL_malloc", referenced from:
_main in main.c.o
"_cblas_dgemm", referenced from:
_main in main.c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
gmake[3]: *** [CMakeFiles/MKLTesting.dir/build.make:84: MKLTesting] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:76: CMakeFiles/MKLTesting.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/MKLTesting.dir/rule] Error 2
gmake: *** [Makefile:118: MKLTesting] Error 2
如果错误代码之一未提供某些详细信息,则我都提供了这两种方法。
我在MacOS Catalina上使用CLion,并且正在C++中编程。
对于如何解决此问题的任何帮助将不胜感激。
谢谢大家的帮助!
最佳答案
我认为您的案件没有问题。我只添加了#include“mkl_cblas.h”而不是“clbas.h”。
然后像MKL Linker Adviser推荐的那样从命令行链接到英特尔mkl:
-L $ {MKLROOT}/lib -Wl,-rpath,$ {MKLROOT}/lib -lmkl_intel_lp64 -lmkl_sequential -lmkl_core -lpthread -lm -ldl
然后启动可执行文件,我看到:
./a.out
11.000000 -9.000000 5.000000 -9.000000 21.000000 -1.000000 5.000000 -1.000000 3.000000
关于c++ - 编译OpenBLAS C++项目给出错误 “Undefined symbols for architecture x86_64”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59235478/