Click here to retrieve the most recent source code for compiling the mex-file on your own, along with some additional m-files required to run mexnc. If you just want to get a mex-file and not have to worry about compiling anything, then you are in the wrong place and need to go back to the main downloads web page. Compiling the mex-file is easiest on UNIX and UNIX-like platforms.
Windows users have a particularly daunting road ahead of them if they wish to compile the mex-file themselves. But hey, if you are feeling particularly brave and have Microsoft Visual Studio, then go ahead and give it a try, knock yourself out.
But for the rest of you, first thing is to copy the mexopts.sh file out of your matlab installation
directory into the directory where you unpacked mexnc. In most cases
you will only need to insert 5 lines into the bottom section of the mexopts.sh file, as indicated in many examples below,
and the ONLY line that would ever really be different is the NETCDF
variable which specifies where netCDF is installed on your system.
Type "make" and that should do it. There are exceptions, though...
NETCDF="/sw"
% setenv CC gcc % setenv CXX % setenv FC % setenv F77 % ./configure --prefix=/Applications/net_cdf_install % make % make installThe modifications to
mexopts.sh are to make the bottom
of the file look like
#############################################################################
#
# Architecture independent lines:
#
# Set and uncomment any lines which will apply to all architectures.
#
#----------------------------------------------------------------------------
NETCDF="/Applications/net_cdf_install"
EXTRA_CFLAGS="-I${NETCDF}/include"
EXTRA_CLIBS="-L${NETCDF}/lib -lnetcdf "
CFLAGS="-g $CFLAGS ${EXTRA_CFLAGS}"
CLIBS="$CLIBS ${EXTRA_CLIBS} "
#----------------------------------------------------------------------------
#############################################################################
/opt/compiled/gnu/netcdf-3.6.0-p1,
so the only modifications to mexopts.sh are to make the bottom
of the file look like
#############################################################################
#
# Architecture independent lines:
#
# Set and uncomment any lines which will apply to all architectures.
#
#----------------------------------------------------------------------------
NETCDF="/opt/compiled/gnu/netcdf-3.6.0-p1"
EXTRA_CFLAGS="-I${NETCDF}/include"
EXTRA_CLIBS="-L${NETCDF}/lib -lnetcdf "
CFLAGS="-g $CFLAGS ${EXTRA_CFLAGS}"
CLIBS="$CLIBS ${EXTRA_CLIBS} "
#----------------------------------------------------------------------------
#############################################################################
/opt/compiled/gnu/netcdf,
so the modifications to mexopts.sh are to make the bottom
of the file look like
#############################################################################
#
# Architecture independent lines:
#
# Set and uncomment any lines which will apply to all architectures.
#
#----------------------------------------------------------------------------
NETCDF="/opt/compiled/gnu/netcdf"
EXTRA_CFLAGS="-I${NETCDF}/include"
EXTRA_CLIBS="-L${NETCDF}/lib -lnetcdf "
CFLAGS="-g $CFLAGS ${EXTRA_CFLAGS}"
CLIBS="$CLIBS ${EXTRA_CLIBS} "
#----------------------------------------------------------------------------
#############################################################################
But attempting to run mexnc results in error messages like
??? Invalid MEX-file '/home/jevans/matlab/mexcdf/mexnc/mexnc.mexglx': /opt/matlabR14sp2/bin/glnx86/../../sys/os/glnx86/libgcc_s.so.1: version `GCC_3.3' not found (required by /usr/lib/libstdc++.so.6).The problem lies in some of the libraries supplied by Mathworks in
${MATLAB}/sys/os/glnx86/
The file ${MATLAB}/sys/os/glnx86/README.libstdc++ says
This version of the C++ library comes with gcc-3.2.3, available (among other places) from ftp.gnu.org. It is included with MATLAB in case your distribution doesn't provide it.
If you need the header files to go with it (particularly if you are building C++ mex files) please visit ftp://ftp.gnu.org/pub/gnu/gcc and download the 3.2.3 compiler sources.
If you have a newer version of libstdc++ on your system already, typically in /usr/lib, you may be able to remove or rename this file and let MATLAB find the one on your own system. If you have problems with this approach, however, we recommend using the same compiler that was used to compile MATLAB, gcc/g++ 3.2.3So here's what I did ...
# cd ${MATLAB}/sys/os/glnx86
# mkdir old
# mv libg2c.so.0 libg2c.so.0.0.0 libgcc_s.so.1 libstdc++.so.5 libstdc++.so.5.0.3 old
After performing this step and recompiling, mexnc runs just fine.
/home/jevans/local,
so if we modify mexopts.sh to make the bottom look like
#############################################################################
#
# Architecture independent lines:
#
# Set and uncomment any lines which will apply to all architectures.
#
#----------------------------------------------------------------------------
NETCDF="/home/jevans/local
EXTRA_CFLAGS="-I${NETCDF}/include"
EXTRA_CLIBS="-L${NETCDF}/lib -lnetcdf "
CFLAGS="-g $CFLAGS ${EXTRA_CFLAGS}"
CLIBS="$CLIBS ${EXTRA_CLIBS} "
#----------------------------------------------------------------------------
#############################################################################
unfortunately, this results in an error message
-> gcc -c -I/opt/matlab.r14/extern/include -DMATLAB_MEX_FILE -g -fPIC -ansi -D_GNU_SOURCE -pthread -fexceptions -m32 -Wall -I/home/jevans/local/include -g mexgateway.c
cc1: Invalid option `32'
mex: compile of 'mexgateway.c' failed.
make: *** [R14] Error 1
By looking thru the mexopts.sh file, we see that starting on line 81, we have
#----------------------------------------------------------------------------
;;
glnx86)
#----------------------------------------------------------------------------
RPATH="-Wl,--rpath-link,$TMW_ROOT/bin/$Arch"
# gcc -v
# gcc version 3.2.3
CC='gcc'
CFLAGS='-fPIC -ansi -D_GNU_SOURCE -pthread -fexceptions -m32 -Wall'
Change that to remove the -m32 flag...
#----------------------------------------------------------------------------
;;
glnx86)
#----------------------------------------------------------------------------
RPATH="-Wl,--rpath-link,$TMW_ROOT/bin/$Arch"
# gcc -v
# gcc version 3.2.3
CC='gcc'
CFLAGS='-fPIC -ansi -D_GNU_SOURCE -pthread -fexceptions -Wall'
and it works, despite gcc being at a much lower revision than the recommended 3.2.3.
On this system, netCDF is located in /opt/gnusoft/netcdf,
so the only modifications to mexopts.sh are to make the bottom
of the file look like
#############################################################################
#
# Architecture independent lines:
#
# Set and uncomment any lines which will apply to all architectures.
#
#----------------------------------------------------------------------------
NETCDF="/opt/gnusoft/netcdf"
EXTRA_CFLAGS="-I${NETCDF}/include"
EXTRA_CLIBS="-L${NETCDF}/lib -lnetcdf "
CFLAGS="-g $CFLAGS ${EXTRA_CFLAGS}"
CLIBS="$CLIBS ${EXTRA_CLIBS} "
#----------------------------------------------------------------------------
#############################################################################
If the link stage gives you an error that looks something like
/usr/bin/ld: ${NETCDF}/lib/libnetcdf.a(attr.o): relocation R_X86_64_32S against `a local symbol' can not be used when making a shared object; recompile with -fPIC
${NETCDF}/lib/libnetcdf.a: could not read symbols: Bad value
collect2: ld returned 1 exit status
mex: link of 'mexnc.mexa64' failed.
then you need to recompile netCDF with the -fPIC flag.
/opt/gnusoft/netcdf.
The default installation of gcc is version 4.0.1-5mdk, but some linux
systems are still below version 4. Since this can cause problems,
let's try to compile it with a non-default version of gcc. The only
difference here is to supply the specified compiler. The modifications to
mexopts.sh are to change the x86-64 section from
#----------------------------------------------------------------------------
;;
glnxa64)
#----------------------------------------------------------------------------
RPATH="-Wl,--rpath-link,$TMW_ROOT/bin/$Arch"
CC='gcc'
to
#----------------------------------------------------------------------------
;;
glnxa64)
#----------------------------------------------------------------------------
RPATH="-Wl,--rpath-link,$TMW_ROOT/bin/$Arch"
CC="gcc-3.3.6"
and modify the bottom of the file to look like
#############################################################################
#
# Architecture independent lines:
#
# Set and uncomment any lines which will apply to all architectures.
#
#----------------------------------------------------------------------------
NETCDF="/opt/gnusoft/netcdf"
EXTRA_CFLAGS="-I${NETCDF}/include"
EXTRA_CLIBS="-L${NETCDF}/lib -lnetcdf "
CFLAGS="-g $CFLAGS ${EXTRA_CFLAGS}"
CLIBS="$CLIBS ${EXTRA_CLIBS} "
#----------------------------------------------------------------------------
#############################################################################
#############################################################################
#
# Architecture independent lines:
#
# Set and uncomment any lines which will apply to all architectures.
#
#----------------------------------------------------------------------------
NETCDF="/opt/gnusoft/netcdf-3.6.0-p1-m32"
EXTRA_CFLAGS="-m32 -I${NETCDF}/include"
EXTRA_CLIBS="-L${NETCDF}/lib -lnetcdf "
CFLAGS="-g $CFLAGS ${EXTRA_CFLAGS}"
CLIBS="$CLIBS ${EXTRA_CLIBS}"
LDFLAGS="-m32 $LDFLAGS "
#----------------------------------------------------------------------------
#############################################################################
First of all, a 32-bit version of the netCDF library is required. Otherwise, the link step will fail with a message like
/usr/bin/ld: skipping incompatible /opt/gnusoft/netcdf-3.6.0-p1/lib/libnetcdf.a when searching for -lnetcdf
/usr/bin/ld: cannot find -lnetcdf
collect2: ld returned 1 exit status
mex: link of 'mexnc.mexglx' failed.
Compiling a 32-bit version of netCDF on an x86-64 machine requires the CFLAGS environment variable to include -m32.
The -m32 flag must also be supplied as part of EXTRA_CFLAGS and LDFLAGS in order to produce 32-bit object files to link against 32-bit matlab. Otherwise the following errors arise at the link stage
/usr/bin/ld: warning: i386:x86-64 architecture of input file `mexgateway.o' is incompatible with i386 output /usr/bin/ld: warning: i386:x86-64 architecture of input file `netcdf2.o' is incompatible with i386 output /usr/bin/ld: warning: i386:x86-64 architecture of input file `netcdf3.o' is incompatible with i386 output /usr/bin/ld: warning: i386:x86-64 architecture of input file `common.o' is incompatible with i386 output /usr/bin/ld: warning: i386:x86-64 architecture of input file `mexversion.o' is incompatible with i386 outputor
/usr/bin/ld: skipping incompatible /opt/tmp/ml/matlabR13/bin/glnx86/libmx.so when searching for -lmx
/usr/bin/ld: cannot find -lmx
collect2: ld returned 1 exit status
mex: link of 'mexnc.mexglx' failed.
make: *** [all] Error 1
/home/jevans/local/ahab/netcdf-3.6.0-p1.
Despite the fact that we don't use gcc as we do in examples above, the mexopts.sh
file is smart enough to know that cc is the default on solaris, not gcc,
and it supplies it automatically.
The only modifications to mexopts.sh are to make the bottom
of the file look like
#############################################################################
#
# Architecture independent lines:
#
# Set and uncomment any lines which will apply to all architectures.
#
#----------------------------------------------------------------------------
NETCDF="/home/jevans/local/ahab/netcdf-3.6.0-p1"
EXTRA_CFLAGS="-I${NETCDF}/include"
EXTRA_CLIBS="-L${NETCDF}/lib -lnetcdf "
CFLAGS="-g $CFLAGS ${EXTRA_CFLAGS}"
CLIBS="$CLIBS ${EXTRA_CLIBS} "
#----------------------------------------------------------------------------
#############################################################################
gcc -v Reading specs from /usr/local/bin/../lib/gcc-lib/sparc-sun-solaris2.9/3.3.2/specs Configured with: ../configure --enable-shared --enable-threads --with-as=/usr/ccs/bin/as --with-ld=/usr/ccs/bin/ld --disable-multilib --disable-libgcj --disable-libffi --disable-libjava --disable-nls --prefix=/usr/local/gcc3 sparc-sun-solaris2.9 Thread model: posix gcc version 3.3.2
/home/jevans/local/ahab/netcdf-3.6.2-beta2.
Normally, solaris would compile mexnc with cc instead of gcc, but what if we don't
have cc? So, grab a copy of gccopts.sh from the matlab installation
directory and rename it to mexopts.sh in the current directory.
The modifications to mexopts.sh that worked were the usual.
#############################################################################
#
# Architecture independent lines:
#
# Set and uncomment any lines which will apply to all architectures.
#
#----------------------------------------------------------------------------
NETCDF="/home/jevans/local/ahab/netcdf-3.6.2-beta2"
EXTRA_CFLAGS="-I${NETCDF}/include"
EXTRA_CLIBS="-L${NETCDF}/lib -lnetcdf "
CFLAGS="-g $CFLAGS ${EXTRA_CFLAGS}"
CLIBS="$CLIBS ${EXTRA_CLIBS} "
#----------------------------------------------------------------------------
#############################################################################
If you get relocation errors in the link stage, it means you need to go
back and use CFLAGS=-fPIC when compiling the netCDF library.
Note, this seemed to work on SunOS 5.9 with R13, but I was unable to get it to work on SunOS 5.8 with R14.
If you are on a windows system, you should take a look at the
win32_make_mexnc.m
m-file.
For information on compiling netCDF, go to Unidata's NetCDF website.