因为使用了别人的项目文件,需要重新编译matlab接口
其一:xxx1.cpp xxx1.cu
其二:xxx2.cu
别人是在windows下用的,使用mexcuda编译出.mexw64文件,我需要在自己的ubuntu16.04系统下编译出.mexa64文件。
最优方法:mexcuda 使用方法:mexcuda filename 但是我没有成功。
曲线救国:nvcc+mex (参考博客 Matlab 调用CUDA的总结(Windows、Ubuntu))
下面说一下曲线救国具体方法
1 使用nvcc编译出.o文件
其一:nvcc -I /usr/local/MATLAB/R2016b/extern/include -c --compiler-options -fPIC xxx2.cu
其二:nvcc -I /usr/local/MATLAB/R2016b/extern/include -c --compiler-options -fPIC xxx1.cpp xxx1.cu(compiler前是两个短横线)
解释一下上面参数的目的
-I /usr/local/MATLAB/R2016b/extern/include: 为了给nvcc包含cpp和cu文件的头文件mex.h的路径(参考博客 nvcc编译器选项及配置)
–compiler-options -fPIC :为了解决编译的.o文件的地址问题
2 在matlab命令行窗口下编译.o文件
其一:mex xxx2.o -L/usr/local/cuda/lib64 -lcudart
其二:mex xxx1.o -L/usr/local/cuda/lib64 -lcudart
解释一下上面参数的目的
-L/usr/local/cuda/lib64 -lcudart:mex编译.o文件报错(参考博客 Compiling error: undefined reference to `cudaMalloc’)
补充说明
1 对于cpp文件和cu文件的头文件,在自己的系统下修改了路径,主要是cuda的两个文件cuda_runtime.h、device_launch_parameters.h的包含路径
#include “/usr/local/cuda-8.0/include/cuda_runtime.h”
#include “/usr/local/cuda-8.0/include/device_launch_parameters.h”
2 编译之前,gcc g++软链接修改到了4.7版本,cc没有修改
又编译了一个文件,提示错误及解决办法如下
错误 #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
添加编译选项 nvcc -std=c++11 -I /usr/local/MATLAB/R2016b/extern/include -c --compiler-options -fPIC xxx3.cu