c++ - BLAS与CMake链接错误

标签 c++ c cmake blas

我有以下 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",正如我在其他网站上看到的那样。

编辑:运行 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)

关于c++ - BLAS与CMake链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25983910/

相关文章:

c++ - 未从命令行输入内容时 Argv 的值

c++ - 如何显示功能的开始,结束行和功能体?

C - 访问结构中的动态数组

C 字符串输入混淆

c++ - Linux - 动态和静态加载时调用相同的函数

c++ - CMakeLists.txt :56 (include): include could not find load file: gmxVersionInfo 处的 CMake 错误

c++ - 为什么将可视化调试器附加到我的程序比直接从 visual studio 运行它更快?

C++ : adding an object to a set

c - 如何定义排除某组词的文法?

c++ - 我可以在带有 Cmake 2.8.12.2 的 ubuntu 14.04 上为 C++ 使用 biicode 吗?