Matlab mex code not compiling using a Makefile - Undefined Reference - matlab

I have a matlab mex extension which I want to compile using a Makefile. The Linker does not seem to find the mex libraries. Here is the Makefile:
MEXSUFFIX = mexa64
IX = mexa64
MATLABHOME = /usr/local/MATLAB/R2013b
MEX = g++
MEXCXX = echo
CXX = g++
CFLAGS = -fPIC -pthread -DMATLAB_MEX_FILE -ansi -D_GNU_SOURCE -fno-omit-frame-pointer -pthread -O3 -DNDEBUG
LIBS = -lm
INCLUDE = -I$(MATLABHOME)/extern/include -Icommon
#g++
MEXFLAGS = -shared -Wl,--no-undefined -Wl,-rpath-link,$(MATLABHOME)/bin/glnxa64 -L$(MATLABHOME)/bin/glnxa64 -lmx -lmex -lmat -lm
PROJECTS = residualfm
MEXDIR = ..
all: $(PROJECTS)
residualfm: residualfm/functions.o
$(MEX) $(MEXFLAGS) $(LIBS) -o $(MEXDIR)/$#.$(MEXSUFFIX) $^
.cpp.o:
$(CXX) -c -o $# $< $(CFLAGS) $(INCLUDE)
clean:
rm -f common/*.o
for proj in $(PROJECTS); do \
rm -f $$proj/*.o; \
rm -f $(MEXDIR)/$$proj.$(MEXSUFFIX); \
done
and here my filestructure:
$ ls *
Makefile mex.kdev4 sfr_mex.sln sfr_mex.suo sfr_mex.v11.suo
residualfm:
DllMain.cpp residualfm_variant.def stdafx.h
functions.cpp residualfm_variant.vcxproj timing.cpp
functions.o residualfm_variant.vcxproj.user
maxflow_classic_boykov stdafx.cpp
Here is the error:
$ make
g++ -shared -Wl,--no-undefined -Wl,-rpath-link,/usr/local/MATLAB/R2013b/bin/glnxa64 -L/usr/local/MATLAB/R2013b/bin/glnxa64 -lmx -lmex -lmat -lm -lm -o ../residualfm.mexa64 residualfm/functions.o
residualfm/functions.o: In function `mexFunction':
functions.cpp:(.text+0x27b): undefined reference to `mexErrMsgTxt'
functions.cpp:(.text+0x2a0): undefined reference to `mexErrMsgTxt'
functions.cpp:(.text+0x2c7): undefined reference to `mxGetClassID'
functions.cpp:(.text+0x2e5): undefined reference to `mexErrMsgTxt'
functions.cpp:(.text+0x301): undefined reference to `mxGetNumberOfDimensions'
functions.cpp:(.text+0x320): undefined reference to `mexErrMsgTxt'
functions.cpp:(.text+0x341): undefined reference to `mxGetData'
functions.cpp:(.text+0x350): undefined reference to `mxGetDimensions'
functions.cpp:(.text+0x371): undefined reference to `mxGetNumberOfElements'
functions.cpp:(.text+0x390): undefined reference to `mexErrMsgTxt'
functions.cpp:(.text+0x3aa): undefined reference to `mxGetScalar'
functions.cpp:(.text+0x3c5): undefined reference to `mxGetNumberOfElements'
functions.cpp:(.text+0x3e4): undefined reference to `mexErrMsgTxt'
functions.cpp:(.text+0x3fe): undefined reference to `mxGetScalar'
functions.cpp:(.text+0x4f5): undefined reference to `mexErrMsgTxt'
functions.cpp:(.text+0xdd3): undefined reference to `mxCreateNumericArray'
functions.cpp:(.text+0xdde): undefined reference to `mxGetData'
functions.cpp:(.text+0xefe): undefined reference to `mxGetScalar'
functions.cpp:(.text+0xf9d): undefined reference to `mxCreateNumericArray'
functions.cpp:(.text+0xfa8): undefined reference to `mxGetData'
functions.cpp:(.text+0x11dc): undefined reference to `mxCreateSparse'
functions.cpp:(.text+0x11e7): undefined reference to `mxGetJc'
functions.cpp:(.text+0x11f2): undefined reference to `mxGetIr'
functions.cpp:(.text+0x11fd): undefined reference to `mxGetPr'
residualfm/functions.o: In function `visit_backwards(Node*, Node*, double*, int)':
functions.cpp:(.text+0x232): undefined reference to `mexErrMsgTxt'
collect2: error: ld returned 1 exit status
make: *** [residualfm] Error 1
Compiling from matlab using mex functions.cpp works fine
edit:
Thanks for the hints!
Solved (but not understood):
Changing the order in residualfm: residualfm/functions.o from:
$(MEX) $(MEXFLAGS) $(LIBS) -o $(MEXDIR)/$#.$(MEXSUFFIX) $^
to
$(MEX) -o $(MEXDIR)/$#.$(MEXSUFFIX) $^ $() $(MEXFLAGS) $(LIBS)
solved the problem. Can someone explain this?

(Solved in the comments and edits. See Question with no answers, but issue solved in the comments (or extended in chat) )
#Amro wrote:
Linked libraries must come after the file being compiled in the command-line
The OP wrote:
Changing the order in residualfm: residualfm/functions.o from:
$(MEX) $(MEXFLAGS) $(LIBS) -o $(MEXDIR)/$#.$(MEXSUFFIX) $^
to
$(MEX) -o $(MEXDIR)/$#.$(MEXSUFFIX) $^ $() $(MEXFLAGS) $(LIBS)
solved the problem.

Related

rpmbuild bad exit status error while creating binary RPM

It is my first time creating RPM package and I followed this documentation to build a simple RPM package. I created source RPM but I can't create binary RPM.
Here is the result when I execute rpmbuild --rebuild /SRPMS/bello-0.1.1.el8.src.rpm:
Installing ../SRPMS/bello-0.1-1.el8.src.rpm
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.LmHvbG
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd /root/rpmbuild/BUILD
+ rm -rf bello-0.1
+ /usr/bin/gzip -dc /root/rpmbuild/SOURCES/bello-0.1.tar.gz
+ /usr/bin/tar -xof -
+ STATUS=0
+ '[' 0 -ne 0 ']'
+ cd bello-0.1
+ /usr/bin/chmod -Rf a+rX,u+w,g-w,o-w .
+ exit 0
Executing(%build): /bin/sh -e /var/tmp/rpm-tmp.k6UGT9
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd bello-0.1
+ CFLAGS='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection'
+ export CFLAGS
+ CXXFLAGS='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection'
+ export CXXFLAGS
+ FFLAGS='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -I/usr/lib64/gfortran/modules'
+ export FFLAGS
+ FCFLAGS='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -I/usr/lib64/gfortran/modules'
+ export FCFLAGS
+ LDFLAGS='-Wl,-z,relro -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld'
+ export LDFLAGS
+ '[' 1 = 1 ']'
+++ dirname ./configure
++ find . -name config.guess -o -name config.sub
+ '[' 1 = 1 ']'
+ '[' x '!=' 'x-Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld' ']'
++ find . -name ltmain.sh
+ ./configure --build=x86_64-redhat-linux-gnu --host=x86_64-redhat-linux-gnu --program-prefix= --disable-dependency-tracking --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --sysconfdir=/etc --datadir=/usr/share --includedir=/usr/include --libdir=/usr/lib64 --libexecdir=/usr/libexec --localstatedir=/var --sharedstatedir=/var/lib --mandir=/usr/share/man --infodir=/usr/share/info
/var/tmp/rpm-tmp.k6UGT9: line 49: ./configure: No such file or directory
error: Bad exit status from /var/tmp/rpm-tmp.k6UGT9 (%build)
RPM build errors:
Bad exit status from /var/tmp/rpm-tmp.k6UGT9 (%build)
I get the same result when I execute rpmbuild -bb bello.spec command.
Here is my SPEC file:
Name: bello
Version: 0.1
Release: 1%{?dist}
Summary: Hello World example implemented in bash script
License: GPLv3+
URL: https://example.com/%{name}
Source0: https://example.com/%{name}/releases/%{name}-%{version}.tar.gz
Requires: bash
BuildArch: noarch
%description
some description for this basic code.
%prep
%setup -q
%build
%configure
%make_build
%install
mkdir -p %{buildroot}/%{_bindir}
install -m 0755 %{name} %{buildroot}/%{_bindir}/%{name}
%make_install
%files
%{_bindir}/%{name}
%license LICENSE
%doc add-docs-here
%changelog
* Tue Aug 30 2022 talat ayhan <fadedship#gmail.com> - 0.1-1
- First bello package
- Example second item in the changelog for version-release 0.1-1

Why make utility cannot find header file?

I have been working in Eclipse based IDE called Vitis (IDE from Xilinx).
I have an automatically generated software project containing three automatically
generated Makefiles. Organization of the project is following:
zynq_fsbl
-Makefile_level_01
-main.c
-zynq_fsbl_bsp
-Makefile_level_02
-ps7_cortexa9_0
-code
-include
-Config.h
-lib
-libsrc
-peripheral_01
-src
-driver_peripheral_01.c
-driver_peripheral_01.h
-Makefile_level_03_01
-peripheral_02
-src
-driver_peripheral_02.c
-driver_peripheral_02.h
-Makefile_level_03_02
- ... same pattern repeats for other perpherals
Makefile_level_01 contains:
# Auto Generated by Xilinx generate_app. Modify at your own risk
CC := arm-none-eabi-gcc
CC_FLAGS := -MMD -MP -mcpu=cortex-a9 -mfpu=vfpv3 -mfloat-abi=hard
CFLAGS :=
LN_FLAGS := -mcpu=cortex-a9 -mfpu=vfpv3 -mfloat-abi=hard -Wl,-build-id=none -specs=Xilinx.spec -lrsa -Wl,--start-group,-lxil,-lgcc,-lc,--end-group -Wl,--start-group,-lxilffs,-lxil,-lgcc,-lc,--end-group -Wl,--start-group,-lrsa,-lxil,-lgcc,-lc,--end-group -Wl,--gc-sections
c_SOURCES := $(wildcard *.c)
S_SOURCES := $(wildcard *.S)
INCLUDES := $(wildcard *.h)
OBJS := $(patsubst %.c, %.o, $(c_SOURCES))
OBJS += $(patsubst %.S, %.o, $(S_SOURCES))
LSCRIPT := -Tlscript.ld
CURRENT_DIR = $(shell pwd)
DEPFILES := $(patsubst %.o, %.d, $(OBJS))
LIBS := zynq_fsbl_bsp/ps7_cortexa9_0/lib/libxil.a
EXEC := executable.elf
INCLUDEPATH := -Izynq_fsbl_bsp/ps7_cortexa9_0/include -I.
LIBPATH := -Lzynq_fsbl_bsp/ps7_cortexa9_0/lib -L./
all: $(EXEC)
$(EXEC): $(LIBS) $(OBJS) $(INCLUDES)
$(CC) -o $# $(OBJS) $(CC_FLAGS) $(CFLAGS) $(LN_FLAGS) $(LIBPATH) $(LSCRIPT)
$(LIBS):
$(MAKE) -C zynq_fsbl_bsp
%.o:%.c
$(CC) $(CC_FLAGS) $(CFLAGS) -c $< -o $# $(INCLUDEPATH)
%.o:%.S
$(CC) $(CC_FLAGS) $(CFLAGS) -c $< -o $# $(INCLUDEPATH)
%.o:%.s
$(CC) $(CC_FLAGS) $(CFLAGS) -c $< -o $# $(INCLUDEPATH)
clean:
rm -rf $(OBJS) $(LIBS) $(EXEC) *.o
-include $(DEPFILES)
Makefile_level_02 contains:
# Makefile generated by Xilinx.
PROCESSOR = ps7_cortexa9_0
LIBRARIES = ${PROCESSOR}/lib/libxil.a
BSP_MAKEFILES := $(wildcard $(PROCESSOR)/libsrc/*/src/Makefile)
SUBDIRS := $(patsubst %/Makefile, %, $(BSP_MAKEFILES))
ifneq (,$(findstring win,$(RDI_PLATFORM)))
SHELL = CMD
endif
all: libs
#echo 'Finished building libraries'
include: $(addsuffix /make.include,$(SUBDIRS))
libs: $(addsuffix /make.libs,$(SUBDIRS))
clean: $(addsuffix /make.clean,$(SUBDIRS))
$(PROCESSOR)/lib/libxil.a: $(PROCESSOR)/lib/libxil_init.a
cp -f $< $#
%/make.include: $(if $(wildcard $(PROCESSOR)/lib/libxil_init.a),$(PROCESSOR)/lib/libxil.a,)
#echo "Running Make include in $(subst /make.include,,$#)"
$(MAKE) -C $(subst /make.include,,$#) -s include "SHELL=$(SHELL)" "COMPILER=arm-none-eabi-gcc" "ASSEMBLER=arm-none-eabi-as" "ARCHIVER=arm-none-eabi-ar" "COMPILER_FLAGS= -O2 -c" "EXTRA_COMPILER_FLAGS=-mcpu=cortex-a9 -mfpu=vfpv3 -mfloat-abi=hard -nostartfiles -g -Wall -Wextra"
%/make.libs: include
#echo "Running Make libs in $(subst /make.libs,,$#)"
$(MAKE) -C $(subst /make.libs,,$#) -s libs "SHELL=$(SHELL)" "COMPILER=arm-none-eabi-gcc" "ASSEMBLER=arm-none-eabi-as" "ARCHIVER=arm-none-eabi-ar" "COMPILER_FLAGS= -O2 -c" "EXTRA_COMPILER_FLAGS=-mcpu=cortex-a9 -mfpu=vfpv3 -mfloat-abi=hard -nostartfiles -g -Wall -Wextra"
%/make.clean:
$(MAKE) -C $(subst /make.clean,,$#) -s clean
clean:
rm -f ${PROCESSOR}/lib/libxil.a
Makefile_level_03 for peripheral_01 contains:
COMPILER=
ARCHIVER=
CP=cp
COMPILER_FLAGS=
EXTRA_COMPILER_FLAGS=
LIB=libxil.a
LEVEL=0
RELEASEDIR=../../../lib
INCLUDEDIR=../../../include
INCLUDES=-I./. -I${INCLUDEDIR}
INCLUDEFILES=*.h
LIBSOURCES=*.c
OUTS = *.o
libs:
echo "Compiling driver_peripheral_01 ..."
$(COMPILER) $(COMPILER_FLAGS) $(EXTRA_COMPILER_FLAGS) $(INCLUDES) $(LIBSOURCES)
$(ARCHIVER) -r ${RELEASEDIR}/${LIB} ${OUTS}
make clean
include:
${CP} $(INCLUDEFILES) $(INCLUDEDIR)
clean:
rm -rf ${OUTS}
My problem is that I am not able to compile the driver_peripheral_01.c module due to the problem with
Config.h header file inclusion. The "fatal error: Config.h: No such file or directory" is being invoked despite the fact that I have copied the header file in question into the include subdirectory in project directory structure. Does anybody have any idea why the make cannot find the Config.h header file? Thanks for any suggestions.

matching substring beginning with special characters without using external commands

I would like a function, abc123, which is called as:
abc123 -std=c++17 -Wall -lstdc++ -lpthread -o xxx xxx.cpp yyy.cpp
I would like to collect all substrings beginning with "-l" in the arguments.
Here is what I am running into:
%> echo (string match "l*" (string split " " "std=c++17 Wall lstdc++ lpthread o xxx xxx.cpp"))
%> lstdc++ lpthread
This is fine. But the following will result in an error as the hyphons are interpreted as options:
echo (string match "-l*" (string split " " "-std=c++17 -Wall -lstdc++ -lpthread -o xxx xxx.cpp"))
You just need to add -- to separate options from arguments:
string match -- "-l*" (string split -- " " "-std=c++17 -Wall -lstdc++ -lpthread -o xxx xxx.cpp")
# ...........^^.....................^^
-lstdc++
-lpthread

Generic mexopts file for GCC/Clang OSX/Linux [R2014b]

It is a recurrent pain in the ice to set up this blurry mexopts.sh... I would just like a generic configuration that works with all combinations of:
Ubuntu 14+ and OSX 10.10+;
gcc 4.8+ and clang 3.5+
I don't want any dependence to Xcode for the OSX part, I don't mind editing that file manually to change version numbers occasionally, and I don't care about warnings regarding the GCC version.
Here is my chaotic mess so far, hopefully with a little help we can finally get something decent:
#
# mexopts.sh Shell script for configuring MEX-file creation script,
# mex. These options were tested with the specified compiler.
#
# usage: Do not call this file directly; it is sourced by the
# mex shell script. Modify only if you don't like the
# defaults after running mex. No spaces are allowed
# around the '=' in the variable assignment.
#
# Note: For the version of system compiler supported with this release,
# refer to the Supported and Compatible Compiler List at:
# http://www.mathworks.com/support/compilers/current_release/
#
#
# SELECTION_TAGs occur in template option files and are used by MATLAB
# tools, such as mex and mbuild, to determine the purpose of the contents
# of an option file. These tags are only interpreted when preceded by '#'
# and followed by ':'.
#
#SELECTION_TAG_MEX_OPT: Template Options file for building MEX-files
#
# Copyright 1984-2011 The MathWorks, Inc.
# $Revision: 1.78.4.18 $ $Date: 2012/11/15 06:22:54 $
#----------------------------------------------------------------------------
#
TMW_ROOT="$MATLAB"
MFLAGS=''
if [ "$ENTRYPOINT" = "mexLibrary" ]; then
MLIBS="-L$TMW_ROOT/bin/$Arch -lmx -lmex -lmat -lmwservices -lut"
else
MLIBS="-L$TMW_ROOT/bin/$Arch -lmx -lmex -lmat"
fi
case "$Arch" in
Undetermined)
#----------------------------------------------------------------------------
# Change this line if you need to specify the location of the MATLAB
# root directory. The script needs to know where to find utility
# routines so that it can determine the architecture; therefore, this
# assignment needs to be done while the architecture is still
# undetermined.
#----------------------------------------------------------------------------
MATLAB="$MATLAB"
;;
glnx86)
#----------------------------------------------------------------------------
echo "Error: Did not imbed 'options.sh' code"; exit 1 #imbed options.sh glnx86 12
#----------------------------------------------------------------------------
;;
glnxa64_gcc|glnxa64)
#----------------------------------------------------------------------------
RPATH="-Wl,-rpath-link,$TMW_ROOT/bin/$Arch"
#
CC='gcc'
CFLAGS='-ansi -D_GNU_SOURCE'
CFLAGS="$CFLAGS -fexceptions"
CFLAGS="$CFLAGS -fPIC -fno-omit-frame-pointer -pthread"
CLIBS="$RPATH $MLIBS -lm"
COPTIMFLAGS='-O2 -DNDEBUG'
CDEBUGFLAGS='-g'
#
CXX='g++'
CXXFLAGS='-ansi -D_GNU_SOURCE -std=c++0x'
CXXFLAGS="$CXXFLAGS -fPIC -fno-omit-frame-pointer -pthread"
CXXLIBS="$RPATH $MLIBS -lm"
CXXOPTIMFLAGS='-O2 -DNDEBUG'
CXXDEBUGFLAGS='-g'
#
FC='gfortran'
FFLAGS='-fexceptions -fbackslash'
FFLAGS="$FFLAGS -fPIC -fno-omit-frame-pointer"
FLIBS="$RPATH $MLIBS -lm"
FOPTIMFLAGS='-O'
FDEBUGFLAGS='-g'
#
LD="$COMPILER"
LDEXTENSION='.mexa64'
LDFLAGS="-pthread -shared -Wl,--version-script,$TMW_ROOT/extern/lib/$Arch/$MAPFILE -Wl,--no-undefined"
LDOPTIMFLAGS='-O'
LDDEBUGFLAGS='-g'
#
POSTLINK_CMDS=':'
#----------------------------------------------------------------------------
;;
glnxa64_clang)
#----------------------------------------------------------------------------
RPATH="-Wl,-rpath-link,$TMW_ROOT/bin/$Arch"
#
CC='clang'
CFLAGS="-fno-common -fexceptions -fPIC -fno-omit-frame-pointer -pthread"
CLIBS="$MLIBS -lstdc++"
COPTIMFLAGS='-O2 -DNDEBUG'
CDEBUGFLAGS='-g'
#
CXX='clang++'
CXXFLAGS="-fno-common -fexceptions -fPIC -fno-omit-frame-pointer -pthread"
CXXLIBS="$RPATH $MLIBS -lstdc++"
CXXOPTIMFLAGS='-O2 -DNDEBUG'
CXXDEBUGFLAGS='-g'
#
FC='gfortran'
FFLAGS='-fexceptions -fbackslash'
FFLAGS="$FFLAGS -fPIC -fno-omit-frame-pointer"
FLIBS="$RPATH $MLIBS -lm"
FOPTIMFLAGS='-O'
FDEBUGFLAGS='-g'
#
LD="$COMPILER"
LDEXTENSION='.mexa64'
LDFLAGS="-pthread -shared -Wl,--version-script,$TMW_ROOT/extern/lib/$Arch/$MAPFILE -Wl,--no-undefined"
LDOPTIMFLAGS='-O'
LDDEBUGFLAGS='-g'
POSTLINK_CMDS=':'
#----------------------------------------------------------------------------
;;
sol64|mac|maci)
#----------------------------------------------------------------------------
echo "Error: Did not imbed 'options.sh' code"; exit 1 #imbed options.sh sol64 12
#----------------------------------------------------------------------------
;;
maci64_clang|maci64)
#
SDKROOT=""
MACOSX_DEPLOYMENT_TARGET='10.10'
ARCHS='x86_64'
MAC_FLAGS="-arch $ARCHS -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
#
CC='clang'
CFLAGS="-fno-common $MAC_FLAGS"
CFLAGS="$CFLAGS -fexceptions"
CLIBS="$MLIBS -lstdc++"
COPTIMFLAGS='-O2 -DNDEBUG'
CDEBUGFLAGS='-g'
#
CXX='clang++'
CXXFLAGS="-fno-common -fexceptions $MAC_FLAGS"
CXXLIBS="$MLIBS -lstdc++"
CXXOPTIMFLAGS='-O2 -DNDEBUG'
CXXDEBUGFLAGS='-g'
#
FC='gfortran'
FFLAGS='-fexceptions -m64 -fbackslash'
FC_LIBDIR=`$FC -print-file-name=libgfortran.dylib 2>&1 | sed -n '1s/\/*libgfortran\.dylib//p'`
FC_LIBDIR2=`$FC -print-file-name=libgfortranbegin.a 2>&1 | sed -n '1s/\/*libgfortranbegin\.a//p'`
FLIBS="$MLIBS -L$FC_LIBDIR -lgfortran -L$FC_LIBDIR2 -lgfortranbegin"
FOPTIMFLAGS='-O'
FDEBUGFLAGS='-g'
#
LD="$CC"
LDEXTENSION='.mexmaci64'
LDFLAGS="-arch $ARCHS -Wl -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
LDFLAGS="$LDFLAGS -bundle -Wl,-exported_symbols_list,$TMW_ROOT/extern/lib/$Arch/$MAPFILE"
LDOPTIMFLAGS='-O'
LDDEBUGFLAGS='-g'
#
POSTLINK_CMDS=':'
#----------------------------------------------------------------------------
;;
maci64_gcc) # doesn't work
#
SDKROOT=""
MACOSX_DEPLOYMENT_TARGET='10.10'
ARCHS='x86_64'
MAC_FLAGS="-arch $ARCHS -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
RPATH="-Wl,$TMW_ROOT/bin/$Arch"
#
CC='gcc'
CFLAGS='-ansi -D_GNU_SOURCE'
CFLAGS="$CFLAGS -fexceptions $MAC_FLAGS"
CFLAGS="$CFLAGS -fPIC -fno-omit-frame-pointer -pthread"
CLIBS="$RPATH $MLIBS -lm"
COPTIMFLAGS='-O2 -DNDEBUG'
CDEBUGFLAGS='-g'
#
CXX='g++'
CXXFLAGS="-ansi -D_GNU_SOURCE $MAC_FLAGS"
CXXFLAGS="$CXXFLAGS -fPIC -fno-omit-frame-pointer -pthread"
CXXLIBS="$RPATH $MLIBS -lm"
CXXOPTIMFLAGS='-O2 -DNDEBUG'
CXXDEBUGFLAGS='-g'
#
FC='gfortran'
FFLAGS='-fexceptions -fbackslash'
FFLAGS="$FFLAGS -fPIC -fno-omit-frame-pointer"
FLIBS="$RPATH $MLIBS -lm"
FOPTIMFLAGS='-O'
FDEBUGFLAGS='-g'
#
LD="$CC"
LDEXTENSION='.mexmaci64'
LDFLAGS="$MAC_FLAGS -bundle -Wl,$TMW_ROOT/extern/lib/$Arch/$MAPFILE"
LDOPTIMFLAGS='-O'
LDDEBUGFLAGS='-g'
#
POSTLINK_CMDS=':'
#----------------------------------------------------------------------------
;;
esac
#############################################################################
#
# Architecture independent lines:
#
# Set and uncomment any lines which will apply to all architectures.
#
#----------------------------------------------------------------------------
# CC="$CC"
# CFLAGS="$CFLAGS"
# COPTIMFLAGS="$COPTIMFLAGS"
# CDEBUGFLAGS="$CDEBUGFLAGS"
# CLIBS="$CLIBS"
#
# FC="$FC"
# FFLAGS="$FFLAGS"
# FOPTIMFLAGS="$FOPTIMFLAGS"
# FDEBUGFLAGS="$FDEBUGFLAGS"
# FLIBS="$FLIBS"
#
# LD="$LD"
# LDFLAGS="$LDFLAGS"
# LDOPTIMFLAGS="$LDOPTIMFLAGS"
# LDDEBUGFLAGS="$LDDEBUGFLAGS"
#----------------------------------------------------------------------------
#############################################################################
You can see that I'm trying to get a different section for each combination arch_compiler. I can then manually select the compiler I want simply by ORing the case choice with the target architecture.

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