使用cmake FindBLAS链接OpenBLAS

问题描述 投票:0回答:1

我正在使用cmake 3.16,并且我知道cmake支持通过使用OpenBLAS FindBLAS查找(here)

我正在尝试将OpenBLAS链接到我的c ++项目。这是我的CMakeLists.txt

cmake_minimum_required(VERSION 3.15)

project(my_project)

# source file
file(GLOB SOURCES "src/*.cpp")

# executable file
add_executable(main.exe ${SOURCES})

# link openblas
set(BLA_VENDER OpenBLAS)
find_package(BLAS REQUIRED)
if(BLAS_FOUND)
    message("OpenBLAS found.")
    include_directories(${BLAS_INCLUDE_DIRS})
    target_link_libraries(main.exe ${BLAS_LIBRARIES})
endif(BLAS_FOUND)

如果我运行cmake,它将运行find,然后输出OpenBLAS found.。但是,如果我开始编译代码(make VERBOSE=1),则库未链接,因此代码无法编译。这是错误信息:

fatal error: cblas.h: No such file or directory
 #include <cblas.h>
          ^~~~~~~~~
compilation terminated.

我已成功安装OpenBLAS。头文件位于/opt/OpenBLAS/include中,共享库位于/opt/OpenBLAS/lib中。我的操作系统是ubuntu 18.04。

有帮助吗?谢谢!

cmake blas openblas
1个回答
0
投票

感谢Tsyvarev。我发现了问题。

我尝试使用message()打印出变量。

message(${BLAS_LIBRARIES})

哪个给:

/opt/OpenBLAS/lib/libopenblas.so

因此找到了共享库。

但是,对于BLAS_INCLUDE_DIRS,它给出:

message(${BLAS_INCLUDE_DIRS})
CMake Error at CMakeLists.txt:27 (message):
  message called with incorrect number of arguments

结果证明BLAS_INCLUDE_DIRS不在FindBLAS变量中。因此,我手动添加了包含头文件:

set(BLA_VENDER OpenBLAS)
find_package(BLAS REQUIRED)
if(BLAS_FOUND)
    message("OpenBLAS found.")
    include_directories(/opt/OpenBLAS/include/)
    target_link_libraries(main.exe ${BLAS_LIBRARIES})
endif(BLAS_FOUND)

这次它编译没有错误。除了使用include_directories(),也可以尝试find_path()check this)。

© www.soinside.com 2019 - 2024. All rights reserved.