BLAS与CMake的链接错误

Linking error of BLAS with CMake

本文关键字:链接 错误 CMake BLAS      更新时间:2023-10-16

我有以下CMakeLists.txt:

cmake_minimum_required (VERSION 2.6) 
project (MTSOS) 
# Select flags.
set(CMAKE_C_FLAGS "-std=c99") 
# Blas library
find_package( BLAS REQUIRED )
include_directories(${BLAS_INCLUDE_DIR})
add_executable (test_MTSOS test_MTSOS.c barrier_front.c dynamics_front.c 
MTSOS.c csparse.c) 
# Linking CImg dependencies.
target_link_libraries (test_MTSOS m blas ${BLAS_LIBRARIES}) 

当我运行cmake ..:时

-- The C compiler identification is GNU 4.8.2
-- The CXX compiler identification is GNU 4.8.2
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Looking for dgemm_
-- Looking for dgemm_ - found
-- Looking for include file pthread.h
-- Looking for include file pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- A library with BLAS API found.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/jabuntu14/Desktop/MTSOS/build

然而,当我尝试使用make:进行编译时

Scanning dependencies of target test_MTSOS
[ 20%] Building C object CMakeFiles/test_MTSOS.dir/test_MTSOS.c.o
[ 40%] Building C object CMakeFiles/test_MTSOS.dir/barrier_front.c.o
/home/jabuntu14/Desktop/MTSOS/barrier_front.c: In function ‘so_barrier’:
/home/jabuntu14/Desktop/MTSOS/barrier_front.c:83:25: warning: implicit declaration of function ‘dsyr’ [-Wimplicit-function-declaration]
                         dsyr(uplo, &u_size, &alpha, &Df[i*U_size], &one, A, &u_size);
                         ^
[ 60%] Building C object CMakeFiles/test_MTSOS.dir/dynamics_front.c.o
[ 80%] Building C object CMakeFiles/test_MTSOS.dir/MTSOS.c.o
/home/jabuntu14/Desktop/MTSOS/MTSOS.c: In function ‘so_MakeA’:
/home/jabuntu14/Desktop/MTSOS/MTSOS.c:1561:13: warning: implicit declaration of function ‘dgemv’ [-Wimplicit-function-declaration]
             dgemv(chn, &m_m, &n_m, &one, &M_dynamics[i*State_size*State_size], &m_m, &S_prime[i*State_size], &p_m, &zero, datam, &p_m);
             ^
[100%] Building C object CMakeFiles/test_MTSOS.dir/csparse.c.o
Linking C executable test_MTSOS
CMakeFiles/test_MTSOS.dir/barrier_front.c.o: In function `so_barrier':
barrier_front.c:(.text+0x4a7): undefined reference to `dsyr'
CMakeFiles/test_MTSOS.dir/MTSOS.c.o: In function `so_MakeA':
MTSOS.c:(.text+0x5e41): undefined reference to `dgemv'
MTSOS.c:(.text+0x5ee4): undefined reference to `dgemv'
MTSOS.c:(.text+0x5f87): undefined reference to `dgemv'
collect2: error: ld returned 1 exit status
make[2]: *** [test_MTSOS] Error 1
make[1]: *** [CMakeFiles/test_MTSOS.dir/all] Error 2
make: *** [all] Error 2

所以我有一个链接问题,但我不知道为什么。欢迎任何帮助。谢谢

注意:使用GCC在Ubuntu 14.04下工作。在.c文件中,我包含了#include "cblas.h",就像我在其他网站上看到的那样。

EDIT:运行make VERBOSE=1,输出为:

jabuntu14@ubuntu:~/Desktop/MTSOS/build$ make VERBOSE=1
/usr/bin/cmake -H/home/jabuntu14/Desktop/MTSOS -B/home/jabuntu14/Desktop/MTSOS/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/jabuntu14/Desktop/MTSOS/build/CMakeFiles /home/jabuntu14/Desktop/MTSOS/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/jabuntu14/Desktop/MTSOS/build'
make -f CMakeFiles/test_MTSOS.dir/build.make CMakeFiles/test_MTSOS.dir/depend
make[2]: Entering directory `/home/jabuntu14/Desktop/MTSOS/build'
cd /home/jabuntu14/Desktop/MTSOS/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/jabuntu14/Desktop/MTSOS /home/jabuntu14/Desktop/MTSOS /home/jabuntu14/Desktop/MTSOS/build /home/jabuntu14/Desktop/MTSOS/build /home/jabuntu14/Desktop/MTSOS/build/CMakeFiles/test_MTSOS.dir/DependInfo.cmake --color=
make[2]: Leaving directory `/home/jabuntu14/Desktop/MTSOS/build'
make -f CMakeFiles/test_MTSOS.dir/build.make CMakeFiles/test_MTSOS.dir/build
make[2]: Entering directory `/home/jabuntu14/Desktop/MTSOS/build'
Linking C executable test_MTSOS
/usr/bin/cmake -E cmake_link_script CMakeFiles/test_MTSOS.dir/link.txt --verbose=1
/usr/bin/cc  -std=c99    CMakeFiles/test_MTSOS.dir/test_MTSOS.c.o CMakeFiles/test_MTSOS.dir/barrier_front.c.o CMakeFiles/test_MTSOS.dir/dynamics_front.c.o CMakeFiles/test_MTSOS.dir/MTSOS.c.o CMakeFiles/test_MTSOS.dir/csparse.c.o  -o test_MTSOS -rdynamic -lm -lblas -lf77blas -latlas 
CMakeFiles/test_MTSOS.dir/barrier_front.c.o: In function `so_barrier':
barrier_front.c:(.text+0x4a7): undefined reference to `dsyr'
CMakeFiles/test_MTSOS.dir/MTSOS.c.o: In function `so_MakeA':
MTSOS.c:(.text+0x5e41): undefined reference to `dgemv'
MTSOS.c:(.text+0x5ee4): undefined reference to `dgemv'
MTSOS.c:(.text+0x5f87): undefined reference to `dgemv'
collect2: error: ld returned 1 exit status
make[2]: *** [test_MTSOS] Error 1
make[2]: Leaving directory `/home/jabuntu14/Desktop/MTSOS/build'
make[1]: *** [CMakeFiles/test_MTSOS.dir/all] Error 2
make[1]: Leaving directory `/home/jabuntu14/Desktop/MTSOS/build'
make: *** [all] Error 2

标准blas库中的函数是

dgemv_

代替

dgemv

如错误所示,请注意后面的下划线。因此,您的代码中可能有调用dgemv()而不是dgemv_()的内容,请更改它,然后就可以了。

(我遇到了同样的问题,我使用的代码有一些类似#define dgemv_ dgemv的代码)