error LNK2019: unresolved external symbol when trying to MEX a C++ file containing CUDA

5 views (last 30 days)
I’m trying to mex a C++ file that uses CUDA. I have included the folder containing cublas.h etc when calling mex and I’ve linked to the library in my CUDA toolbox folder.
mex -v NIPALS_gpu.cpp -I'C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include' -L'C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\lib\x64'
but when I run it, it produces the error:
Error using mex
Creating library NIPALS_gpu.lib and object NIPALS_gpu.exp
NIPALS_gpu.obj : error LNK2019: unresolved external symbol cublasSetVector referenced in function "void __cdecl cublasSnormalize(int,int,float *,float
*,float *)" (?cublasSnormalize@@YAXHHPEAM00@Z)
.
And about 10 other similar LNK2019 errors.
Does anyone have experience in mexing CUDA and know what the error is about? Do I need to include some additional file that I’ve missed? The file I’m trying to mex ( NIPALS_gpu.cpp) is not something I’ve written, its available online and likely does not contain any errors.

Answers (1)

Joss Knight
Joss Knight on 3 Oct 2016
mex on its own does not automatically link in the CUDA libraries, so you will have to do that manually using -lcublas or similar. Your -L switch has added a library path but hasn't actually added any libraries.
Easier still is to use the MEX wrapper mexcuda which both automatically links in the CUDA libraries, and links in the GPU mex interface so that you can pass MATLAB gpuArray objects into your mex function. (Obviously, this is only available with Parallel Computing Toolbox.)
  2 Comments
Bob Khosh
Bob Khosh on 15 Sep 2018
I do use "mexcuda" but i still get the following error:
Error using mex
Creating library mexMatrixMultiplication.lib and object
mexMatrixMultiplication.exp
mexMatrixMultiplication.obj : error LNK2019: unresolved
external symbol cublasCreate_v2 referenced in function
mexFunction
mexMatrixMultiplication.obj : error LNK2019: unresolved
external symbol cublasDestroy_v2 referenced in function
mexFunction
mexMatrixMultiplication.obj : error LNK2019: unresolved
external symbol cublasSetMatrix referenced in function
mexFunction
mexMatrixMultiplication.obj : error LNK2019: unresolved
external symbol cublasGetMatrix referenced in function
mexFunction
mexMatrixMultiplication.obj : error LNK2019: unresolved
external symbol cublasDgemm_v2 referenced in function
mexFunction
mexMatrixMultiplication.mexw64 : fatal error LNK1120: 5
unresolved externals
Error in mexcuda (line 157)
[varargout{1:nargout}] = mex(mexArguments{:});
Bowen Jia
Bowen Jia on 8 Feb 2020
Edited: Bowen Jia on 8 Feb 2020
Hi, Joss. I met the same question as Bob, is there anyway now to resolve this?
okay, I got this. Just like Joss said, add the "-lcublas" like ''mexcuda matrixmultiplacaiton.cu -lcublas''

Sign in to comment.

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!