简体   繁体   中英

Undefined reference to __cxa_atexit with Matlab/CUDA MEX

I'm compiling some CUDA/C to run as a MEX library in Matlab, and I am getting the following error:

Error using mex
/usr/lib/x86_64-linux-gnu/libc_nonshared.a(atexit.oS): In function `atexit':
(.text+0x12): undefined reference to `__cxa_atexit'
collect2: error: ld returned 1 exit status

I'm assuming this means I need to link in some additional library, but what?

My current compile command is:

mex -L/usr/local/cuda/lib64 -lcudart -I./ A.cpp B.cpp C.cu D.cpp

I have several kernels to compile into mex, and interestingly some of them work and some of them don't. I don't immediately see what the difference is between them - I certainly don't call atexit anywhere in my source.

Here is the output of a verbose mex:

Verbose mode is on.
Neither -compatibleArrayDims nor -largeArrayDims is selected.
     Using -compatibleArrayDims. In the future, MATLAB will require the use of
     -largeArrayDims and remove the -compatibleArrayDims option.
     For more information:
     http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.
... Looking for compiler 'nvcc' ...
... Looking for environment variable 'MW_NVCC_PATH' ...Yes ('/usr/local/cuda-6.5/bin/nvcc').
... Looking for folder '/usr/local/cuda-6.5/bin' ...Yes.
... Looking for file '/usr/local/cuda-6.5/bin/nvcc' ...Yes.
... Executing command 'which g++' ...Yes ('/usr/bin/g++').
... Executing command 'g++ -print-file-name=libstdc++.so' ...Yes ('/usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.so').
Found installed compiler 'nvcc'.
Options file details
-------------------------------------------------------------------
    Compiler location: /usr/local/cuda-6.5/bin/nvcc
    Options file: /home/nick/Documents/FrameLab/matlab/mex/source codes/mex_CUDA_glnxa64.xml
    CMDLINE2 : /usr/bin/g++ -pthread -Wl,--no-undefined  -shared -O -Wl,--version-script,"/usr/local/MATLAB/R2014a/extern/lib/glnxa64/mexFunction.map" /tmp/mex_86382821022260_3849/Atx_fan_mf.o /tmp/mex_86382821022260_3849/Atx_fan_mf_gpu_new.o /tmp/mex_86382821022260_3849/Atx_fan_mf_gpu_new_fb.o /tmp/mex_86382821022260_3849/Atx_fan_mf_cpu_new.o /tmp/mex_86382821022260_3849/Atx_fan_mf_cpu_new_fb.o /tmp/mex_86382821022260_3849/find_l.o /tmp/mex_86382821022260_3849/find_area.o   -lcudart   -L/usr/local/cuda/lib64   -Wl,-rpath-link,/usr/local/MATLAB/R2014a/bin/glnxa64 -L"/usr/local/MATLAB/R2014a/bin/glnxa64" -lmx -lmex -lmat -lm -lmwgpu /usr/local/MATLAB/R2014a/bin/glnxa64/libcudart.so.5.5 -o Atx_fan_mf.mexa64
    CXX : /usr/local/cuda-6.5/bin/nvcc
    DEFINES : -DMX_COMPAT_32   --compiler-options=-D_GNU_SOURCE,-DMATLAB_MEX_FILE
    MATLABMEX : -DMATLAB_MEX_FILE
    NVCCFLAGS : -gencode=arch=compute_30,code=\"sm_30,compute_30\" $NVCC_FLAGS
    CXXFLAGS : --compiler-options=-ansi,-fexceptions,-fPIC,-fno-omit-frame-pointer,-pthread
    INCLUDE : -I./  -I"/usr/local/MATLAB/R2014a/extern/include" -I"/usr/local/MATLAB/R2014a/toolbox/distcomp/gpu/extern/include/"
    CXXOPTIMFLAGS : -O -DNDEBUG
    CXXDEBUGFLAGS : -g
    LDXX : /usr/bin/g++
    LDFLAGS : -pthread -Wl,--no-undefined 
    LDTYPE : -shared
    LINKEXPORT : -Wl,--version-script,"/usr/local/MATLAB/R2014a/extern/lib/glnxa64/mexFunction.map"
    LINKLIBS : -lcudart   -L/usr/local/cuda/lib64   -Wl,-rpath-link,/usr/local/MATLAB/R2014a/bin/glnxa64 -L"/usr/local/MATLAB/R2014a/bin/glnxa64" -lmx -lmex -lmat -lm -lmwgpu /usr/local/MATLAB/R2014a/bin/glnxa64/libcudart.so.5.5
    LDOPTIMFLAGS : -O
    LDDEBUGFLAGS : -g
    OBJEXT : .o
    LDEXT : .mexa64
    NVCC : /usr/local/cuda-6.5/bin/nvcc
    GCC : /usr/bin/g++
    CPPLIB_DIR : /usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.so
    MATLABROOT : /usr/local/MATLAB/R2014a
    ARCH : glnxa64
    SRC : "/home/nick/Documents/FrameLab/matlab/mex/source codes/Atx_fan_mf.cpp";"/home/nick/Documents/FrameLab/matlab/mex/source codes/Atx_fan_mf_gpu_new.cu";"/home/nick/Documents/FrameLab/matlab/mex/source codes/Atx_fan_mf_gpu_new_fb.cu";"/home/nick/Documents/FrameLab/matlab/mex/source codes/Atx_fan_mf_cpu_new.cpp";"/home/nick/Documents/FrameLab/matlab/mex/source codes/Atx_fan_mf_cpu_new_fb.cpp";"/home/nick/Documents/FrameLab/matlab/mex/source codes/find_l.cpp";"/home/nick/Documents/FrameLab/matlab/mex/source codes/find_area.cpp"
    OBJ : /tmp/mex_86382821022260_3849/Atx_fan_mf.o;/tmp/mex_86382821022260_3849/Atx_fan_mf_gpu_new.o;/tmp/mex_86382821022260_3849/Atx_fan_mf_gpu_new_fb.o;/tmp/mex_86382821022260_3849/Atx_fan_mf_cpu_new.o;/tmp/mex_86382821022260_3849/Atx_fan_mf_cpu_new_fb.o;/tmp/mex_86382821022260_3849/find_l.o;/tmp/mex_86382821022260_3849/find_area.o
    OBJS : /tmp/mex_86382821022260_3849/Atx_fan_mf.o /tmp/mex_86382821022260_3849/Atx_fan_mf_gpu_new.o /tmp/mex_86382821022260_3849/Atx_fan_mf_gpu_new_fb.o /tmp/mex_86382821022260_3849/Atx_fan_mf_cpu_new.o /tmp/mex_86382821022260_3849/Atx_fan_mf_cpu_new_fb.o /tmp/mex_86382821022260_3849/find_l.o /tmp/mex_86382821022260_3849/find_area.o 
    SRCROOT : /home/nick/Documents/FrameLab/matlab/mex/source codes/Atx_fan_mf
    DEF : /tmp/mex_86382821022260_3849/Atx_fan_mf.def
    EXP : Atx_fan_mf.exp
    LIB : Atx_fan_mf.lib
    EXE : Atx_fan_mf.mexa64
    ILK : Atx_fan_mf.ilk
    MANIFEST : Atx_fan_mf.mexa64.manifest
    TEMPNAME : Atx_fan_mf
    EXEDIR : 
    EXENAME : Atx_fan_mf
    OPTIM : -O -DNDEBUG
    LINKOPTIM : -O
    CMDLINE1_0 : /usr/local/cuda-6.5/bin/nvcc -c -DMX_COMPAT_32   --compiler-options=-D_GNU_SOURCE,-DMATLAB_MEX_FILE -I./  -I"/usr/local/MATLAB/R2014a/extern/include" -I"/usr/local/MATLAB/R2014a/toolbox/distcomp/gpu/extern/include/" -gencode=arch=compute_30,code=\"sm_30,compute_30\" $NVCC_FLAGS --compiler-options=-ansi,-fexceptions,-fPIC,-fno-omit-frame-pointer,-pthread -O -DNDEBUG "/home/nick/Documents/FrameLab/matlab/mex/source codes/Atx_fan_mf.cpp" -o /tmp/mex_86382821022260_3849/Atx_fan_mf.o
    CMDLINE1_1 : /usr/local/cuda-6.5/bin/nvcc -c -DMX_COMPAT_32   --compiler-options=-D_GNU_SOURCE,-DMATLAB_MEX_FILE -I./  -I"/usr/local/MATLAB/R2014a/extern/include" -I"/usr/local/MATLAB/R2014a/toolbox/distcomp/gpu/extern/include/" -gencode=arch=compute_30,code=\"sm_30,compute_30\" $NVCC_FLAGS --compiler-options=-ansi,-fexceptions,-fPIC,-fno-omit-frame-pointer,-pthread -O -DNDEBUG "/home/nick/Documents/FrameLab/matlab/mex/source codes/Atx_fan_mf_gpu_new.cu" -o /tmp/mex_86382821022260_3849/Atx_fan_mf_gpu_new.o
    CMDLINE1_2 : /usr/local/cuda-6.5/bin/nvcc -c -DMX_COMPAT_32   --compiler-options=-D_GNU_SOURCE,-DMATLAB_MEX_FILE -I./  -I"/usr/local/MATLAB/R2014a/extern/include" -I"/usr/local/MATLAB/R2014a/toolbox/distcomp/gpu/extern/include/" -gencode=arch=compute_30,code=\"sm_30,compute_30\" $NVCC_FLAGS --compiler-options=-ansi,-fexceptions,-fPIC,-fno-omit-frame-pointer,-pthread -O -DNDEBUG "/home/nick/Documents/FrameLab/matlab/mex/source codes/Atx_fan_mf_gpu_new_fb.cu" -o /tmp/mex_86382821022260_3849/Atx_fan_mf_gpu_new_fb.o
    CMDLINE1_3 : /usr/local/cuda-6.5/bin/nvcc -c -DMX_COMPAT_32   --compiler-options=-D_GNU_SOURCE,-DMATLAB_MEX_FILE -I./  -I"/usr/local/MATLAB/R2014a/extern/include" -I"/usr/local/MATLAB/R2014a/toolbox/distcomp/gpu/extern/include/" -gencode=arch=compute_30,code=\"sm_30,compute_30\" $NVCC_FLAGS --compiler-options=-ansi,-fexceptions,-fPIC,-fno-omit-frame-pointer,-pthread -O -DNDEBUG "/home/nick/Documents/FrameLab/matlab/mex/source codes/Atx_fan_mf_cpu_new.cpp" -o /tmp/mex_86382821022260_3849/Atx_fan_mf_cpu_new.o
    CMDLINE1_4 : /usr/local/cuda-6.5/bin/nvcc -c -DMX_COMPAT_32   --compiler-options=-D_GNU_SOURCE,-DMATLAB_MEX_FILE -I./  -I"/usr/local/MATLAB/R2014a/extern/include" -I"/usr/local/MATLAB/R2014a/toolbox/distcomp/gpu/extern/include/" -gencode=arch=compute_30,code=\"sm_30,compute_30\" $NVCC_FLAGS --compiler-options=-ansi,-fexceptions,-fPIC,-fno-omit-frame-pointer,-pthread -O -DNDEBUG "/home/nick/Documents/FrameLab/matlab/mex/source codes/Atx_fan_mf_cpu_new_fb.cpp" -o /tmp/mex_86382821022260_3849/Atx_fan_mf_cpu_new_fb.o
    CMDLINE1_5 : /usr/local/cuda-6.5/bin/nvcc -c -DMX_COMPAT_32   --compiler-options=-D_GNU_SOURCE,-DMATLAB_MEX_FILE -I./  -I"/usr/local/MATLAB/R2014a/extern/include" -I"/usr/local/MATLAB/R2014a/toolbox/distcomp/gpu/extern/include/" -gencode=arch=compute_30,code=\"sm_30,compute_30\" $NVCC_FLAGS --compiler-options=-ansi,-fexceptions,-fPIC,-fno-omit-frame-pointer,-pthread -O -DNDEBUG "/home/nick/Documents/FrameLab/matlab/mex/source codes/find_l.cpp" -o /tmp/mex_86382821022260_3849/find_l.o
    CMDLINE1_6 : /usr/local/cuda-6.5/bin/nvcc -c -DMX_COMPAT_32   --compiler-options=-D_GNU_SOURCE,-DMATLAB_MEX_FILE -I./  -I"/usr/local/MATLAB/R2014a/extern/include" -I"/usr/local/MATLAB/R2014a/toolbox/distcomp/gpu/extern/include/" -gencode=arch=compute_30,code=\"sm_30,compute_30\" $NVCC_FLAGS --compiler-options=-ansi,-fexceptions,-fPIC,-fno-omit-frame-pointer,-pthread -O -DNDEBUG "/home/nick/Documents/FrameLab/matlab/mex/source codes/find_area.cpp" -o /tmp/mex_86382821022260_3849/find_area.o
    CMDLINE3_0 : rm -f /tmp/mex_86382821022260_3849/Atx_fan_mf.o
    CMDLINE3_1 : rm -f /tmp/mex_86382821022260_3849/Atx_fan_mf_gpu_new.o
    CMDLINE3_2 : rm -f /tmp/mex_86382821022260_3849/Atx_fan_mf_gpu_new_fb.o
    CMDLINE3_3 : rm -f /tmp/mex_86382821022260_3849/Atx_fan_mf_cpu_new.o
    CMDLINE3_4 : rm -f /tmp/mex_86382821022260_3849/Atx_fan_mf_cpu_new_fb.o
    CMDLINE3_5 : rm -f /tmp/mex_86382821022260_3849/find_l.o
    CMDLINE3_6 : rm -f /tmp/mex_86382821022260_3849/find_area.o
-------------------------------------------------------------------
Building with 'nvcc'.
/usr/local/cuda-6.5/bin/nvcc -c -DMX_COMPAT_32   --compiler-options=-D_GNU_SOURCE,-DMATLAB_MEX_FILE -I./  -I"/usr/local/MATLAB/R2014a/extern/include" -I"/usr/local/MATLAB/R2014a/toolbox/distcomp/gpu/extern/include/" -gencode=arch=compute_30,code=\"sm_30,compute_30\" $NVCC_FLAGS --compiler-options=-ansi,-fexceptions,-fPIC,-fno-omit-frame-pointer,-pthread -O -DNDEBUG "/home/nick/Documents/FrameLab/matlab/mex/source codes/Atx_fan_mf.cpp" -o /tmp/mex_86382821022260_3849/Atx_fan_mf.o
/usr/local/cuda-6.5/bin/nvcc -c -DMX_COMPAT_32   --compiler-options=-D_GNU_SOURCE,-DMATLAB_MEX_FILE -I./  -I"/usr/local/MATLAB/R2014a/extern/include" -I"/usr/local/MATLAB/R2014a/toolbox/distcomp/gpu/extern/include/" -gencode=arch=compute_30,code=\"sm_30,compute_30\" $NVCC_FLAGS --compiler-options=-ansi,-fexceptions,-fPIC,-fno-omit-frame-pointer,-pthread -O -DNDEBUG "/home/nick/Documents/FrameLab/matlab/mex/source codes/Atx_fan_mf_gpu_new.cu" -o /tmp/mex_86382821022260_3849/Atx_fan_mf_gpu_new.o
/usr/local/cuda-6.5/bin/nvcc -c -DMX_COMPAT_32   --compiler-options=-D_GNU_SOURCE,-DMATLAB_MEX_FILE -I./  -I"/usr/local/MATLAB/R2014a/extern/include" -I"/usr/local/MATLAB/R2014a/toolbox/distcomp/gpu/extern/include/" -gencode=arch=compute_30,code=\"sm_30,compute_30\" $NVCC_FLAGS --compiler-options=-ansi,-fexceptions,-fPIC,-fno-omit-frame-pointer,-pthread -O -DNDEBUG "/home/nick/Documents/FrameLab/matlab/mex/source codes/Atx_fan_mf_gpu_new_fb.cu" -o /tmp/mex_86382821022260_3849/Atx_fan_mf_gpu_new_fb.o
/usr/local/cuda-6.5/bin/nvcc -c -DMX_COMPAT_32   --compiler-options=-D_GNU_SOURCE,-DMATLAB_MEX_FILE -I./  -I"/usr/local/MATLAB/R2014a/extern/include" -I"/usr/local/MATLAB/R2014a/toolbox/distcomp/gpu/extern/include/" -gencode=arch=compute_30,code=\"sm_30,compute_30\" $NVCC_FLAGS --compiler-options=-ansi,-fexceptions,-fPIC,-fno-omit-frame-pointer,-pthread -O -DNDEBUG "/home/nick/Documents/FrameLab/matlab/mex/source codes/Atx_fan_mf_cpu_new.cpp" -o /tmp/mex_86382821022260_3849/Atx_fan_mf_cpu_new.o
/usr/local/cuda-6.5/bin/nvcc -c -DMX_COMPAT_32   --compiler-options=-D_GNU_SOURCE,-DMATLAB_MEX_FILE -I./  -I"/usr/local/MATLAB/R2014a/extern/include" -I"/usr/local/MATLAB/R2014a/toolbox/distcomp/gpu/extern/include/" -gencode=arch=compute_30,code=\"sm_30,compute_30\" $NVCC_FLAGS --compiler-options=-ansi,-fexceptions,-fPIC,-fno-omit-frame-pointer,-pthread -O -DNDEBUG "/home/nick/Documents/FrameLab/matlab/mex/source codes/Atx_fan_mf_cpu_new_fb.cpp" -o /tmp/mex_86382821022260_3849/Atx_fan_mf_cpu_new_fb.o
/usr/local/cuda-6.5/bin/nvcc -c -DMX_COMPAT_32   --compiler-options=-D_GNU_SOURCE,-DMATLAB_MEX_FILE -I./  -I"/usr/local/MATLAB/R2014a/extern/include" -I"/usr/local/MATLAB/R2014a/toolbox/distcomp/gpu/extern/include/" -gencode=arch=compute_30,code=\"sm_30,compute_30\" $NVCC_FLAGS --compiler-options=-ansi,-fexceptions,-fPIC,-fno-omit-frame-pointer,-pthread -O -DNDEBUG "/home/nick/Documents/FrameLab/matlab/mex/source codes/find_l.cpp" -o /tmp/mex_86382821022260_3849/find_l.o
/usr/local/cuda-6.5/bin/nvcc -c -DMX_COMPAT_32   --compiler-options=-D_GNU_SOURCE,-DMATLAB_MEX_FILE -I./  -I"/usr/local/MATLAB/R2014a/extern/include" -I"/usr/local/MATLAB/R2014a/toolbox/distcomp/gpu/extern/include/" -gencode=arch=compute_30,code=\"sm_30,compute_30\" $NVCC_FLAGS --compiler-options=-ansi,-fexceptions,-fPIC,-fno-omit-frame-pointer,-pthread -O -DNDEBUG "/home/nick/Documents/FrameLab/matlab/mex/source codes/find_area.cpp" -o /tmp/mex_86382821022260_3849/find_area.o
/usr/bin/g++ -pthread -Wl,--no-undefined  -shared -O -Wl,--version-script,"/usr/local/MATLAB/R2014a/extern/lib/glnxa64/mexFunction.map" /tmp/mex_86382821022260_3849/Atx_fan_mf.o /tmp/mex_86382821022260_3849/Atx_fan_mf_gpu_new.o /tmp/mex_86382821022260_3849/Atx_fan_mf_gpu_new_fb.o /tmp/mex_86382821022260_3849/Atx_fan_mf_cpu_new.o /tmp/mex_86382821022260_3849/Atx_fan_mf_cpu_new_fb.o /tmp/mex_86382821022260_3849/find_l.o /tmp/mex_86382821022260_3849/find_area.o   -lcudart   -L/usr/local/cuda/lib64   -Wl,-rpath-link,/usr/local/MATLAB/R2014a/bin/glnxa64 -L"/usr/local/MATLAB/R2014a/bin/glnxa64" -lmx -lmex -lmat -lm -lmwgpu /usr/local/MATLAB/R2014a/bin/glnxa64/libcudart.so.5.5 -o Atx_fan_mf.mexa64

I'm wondering if I should try using a different version of g++ or the c++ libraries? I'm not sure how to change the CPPLIB_DIR variable...Matlab has made it more difficult to mess with mex settings in 2014a.

Thanks for any thoughts!

I got the same error using Matlab 2015 Prerelease on Ubuntu 14.04 with CUDA 6.5 and g++4.8. Thanks to Matlab support we figured out, that manually linking does the trick. Here is my compile script for the matlab mex/gpu example:

myArch = computer('arch'); pathToOpts = fullfile(matlabroot, ... 'toolbox', 'distcomp', 'gpu', ... 'extern', 'src', 'mex', myArch,'gcc',... ['mex_CUDA_' myArch '.xml']); copyfile(pathToOpts,'.','f')

mex -v -largeArrayDims mexGPUExample.cu -lstdc++ -lc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM