Revision as of 13:37, 10 July 2015 by Rjpatruno (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This page contains helpful information regarding compiling/mex issues, SPM, and dealing with software versions. If you've solved a problem, or have found a solution elsewhere, please tap into your altruistic side and post it.

If you encounter a problem not covered in this page, and believe there is a bug in the code, please report it on the Bugs page. Thank you!


[edit] MATLAB

If you are having MATLAB related issues please make sure that you have followed the instructions for setting up MATLAB found on our MATLAB page!

[edit] Compiling/MEX issues

[edit] Mex Files

If you are having problems related to mex files and you're seeing errors that say 'invalid mex file' or something like it you need to follow the directions under the heading "Installing Necessary Libraries.

If you have the VISTASOFT SVN repository then you probably already have the solution.

  • Run mrvInstall

This script will search for and install missing libraries for you.

Many pieces of VISTA software depend on MEX-files. These include mrMesh, Nifti file reading in mrDiffusion, and a number of utilities used for intensive computational steps (interpolation, filtering, etc).

Because these files are compiled as binary files, they are more sensitive to the details of your operating system and CPU. Windows, Mac OS X, and different flavors of unix/linux use different versions of MEX files. Furthermore, on machines with 64-bit CPUs (which are becoming more commonplace), MATLAB can run in either 32-bit or 64-bit mode, which further changes the particular piece of compiled code the software needs. This can make getting these bits of code to run on your machine tricky, although many of the pitfalls have been explored and documented here. (Some basic MATLAB tips: the computer command gives you information on what OS / architecture you are using, while mexext lists the preferred file extensions for MEX-files.)

The compiled version of MEX-files for most combinations of operating system and architecture are included in the VISTASOFT repository. These compiled versions generally work, particularly on version r2006a. However, if these MEX files are not working, the source code for these MEX files is also included. This section includes details on compiling this source code (converting the code from text files of instructions to a binary that will work on your system).

If you see an error like the one below, you have a problem with a MEX-file or library:

    Invalid MEX-file 'c:\user\blah\filename.dll': The specified module could not be found.

Here's a summary of the basics:

  • MEX stands for "Matlab-executable", and a MEX-file is created by compiling code written in C or some other language such that it becomes executable from within MATLAB. Why have a MEX-file instead of a .m file? Either because you've already written it in C, and don't want to translate it, or because the code runs more efficiently this way.
  • You can check whether your MATLAB version is running in 32-bit or 64-bit mode by typing mexext and looking at the last two digits of the output. Your computer may run natively in a different mode from the one that MATLAB reports. For example, MATLAB R2006b runs in 32-bit mode, even on a 64-bit machine.
  • To build a MEX-file -- and more importantly, to build a MEX-file that will work on your machine -- you need the right compiler. Different compilers are appropriate for different combinations of version of MATLAB, operating system, specific version of operating system, and most annoyingly, important differences if you have 32-bit vs 64-bit Windows.
  • You will also need the right libraries. dll stands for dynamic linked library, and is a sort of portable library. If MATLAB doesn't know where a necessary .dll file is, this can also cause a MEX-file error, even if the MEX-file was compiled correctly.

[edit] Installing a Compiler

[edit] Recent Notes

Wandell is the only Windows user in our lab. He has a 64-bit Windows 7 platform. Recently, he installed Visual Studio 2010 C++ and the .NET SDK Windows 7 framekwork. Apparently both are required for mex creation with recent versions of Matlab.

Be aware: There is a known problem when installing Visual Studio 2010.

To fix this bug you must apply this patch. Yes, this is annoying. Don't waste time complaining, Wandell did enough of that on your behalf.

[edit] Earlier Notes

Certain functions may have to be compiled to work on your system. In order to compile these functions you need to install a compiler. These notes were created in the time of Visual Studio 2008.

If you're using a windows 64-bit machine follow the instructions on this page to install a working compiler that is able to compile 64-bit programs. This page details which compilers work with matlab 2009b. This page is a helpful thread on the mathworks page that talks about the issue of 64-bit compilation under matlab.

If you just got a new computer, operating system, or version of MATLAB, and are seeing MEX-file errors right away, chances are you need to re-compile the MEX-file(s) from the original source code.

Type mex -setup to see which compilers you have and to choose the one you want to use.

You might need to get a new compiler (either a free download somewhere, or a purchased compiler). Here ( is one that Bob recommended for Windows machines, although you may also need to install Microsoft Platform SDK to go along with it. For a 64-bit Windows machine running a version of MATLAB with mexw64 extensions, download the SDK. MAKE SURE TO FOLLOW ALL THE STEPS listed on the download site.

An extra thing you have to do, before MATLAB will be able to see the compiler, is to set the environment variables correctly.

  1. Right-click on My Computer
  2. Select Properties
  3. Choose the Advanced tab, and click on the "Environment Variables" button
  4. In the lower window ("System Variables"), click "New".
  5. Call your new variable "mssdk", and for the value, the SDK path (e.g., "C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\")

Finally, tell MATLAB to choose your new compiler by running mex -setup again. This should list a new compiler, "Microsoft Platform SDK 14.0 in C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\". Choose this, and you are now ready to compile using the mex command.

[edit] Re-compile Source Code

Read the source code you are trying to compile (e.g., edit readFileNifti.c). Usually Bob or Tony will have described in the comments how to compile the code on different platforms.

Make sure you have changed to the directory where the code exists before you try to compile.

For example, here are are the comments included in readFileNifti.c:

* To compile on most unices (maybe BSD/OS-X?):
*   mex readFileNifti.c nifti1_io.c znzlib.c
* You might need to explicitly include zlib:
*   mex readFileNifti.c nifti1_io.c znzlib.c /usr/local/matlab/r2007a/bin/glnx86/
* On Windows, try:
*   mex -D_WINDOWS_ -I./win32 readFileNifti.c nifti1_io.c znzlib.c ./win32/zlib.lib
* On Cygwin/gnumex, try:
*   mex readFileNifti.c nifti1_io.c znzlib.c -Ic:\PATHTOZLIBINCLUDE c:\PATHTOZLIB\libz.a

You may still get an error after executing one of the suggested commands. This could mean that you don't have the right compiler, or MATLAB couldn't find the required libraries, etc. The error may look like this:

 ??? Error using ==> mex
 Unable to complete successfully.

[edit] What needs to be compiled?

For mrDiffusion, there is a script called dtiCompileMex.m, which can be found in \VISTASOFT\mrDiffusion\src\. Running this script should compile all the necessary code for mrDiffusion to work, even on 64-bit Windows machines.

For other mrVista software, you should ask someone like Bob or Rory which pieces of code should be recompiled, as there is no master script yet. pnet.c is needed, but has been recompiled to work on 64-bit windows already.

As of August 2007, mrDiffusion code, readFileNifti, and VolumeUtilities are compiled to work on 64-bit Windows with MATLABR2007a. However, upConv.c, corrDn.c, and other files needed to operate mrMesh still need to be recompiled to work with 64-bit Windows. We may be able to find correctly-compiled versions of these on Eero Simoncelli's website under 'matlabPyrTools' (thanks to Ruth Rosenholtz for the tip!).

[edit] Building the VTK mex files

Some of our mex files use the Visualization Tool Kit (VTK) to build (build_mesh) and manipulate (smooth_mesh, curvature) cortical surface manifolds. Compiling these functions takes a little more effort than simple mex files.

Alex Wade has had success building them on 64-bit Windows. To get this working, Alex had to make a few changes:

  1. in grayVolume there were two places where a function was returning a type vtkIdType * but it was being assigned to an int *. I changed this so that the thing it was assigned to was a vtkIdType * (do a CVS diff to see where).
  2. Removed the #pragma comment(linker, "/NODEFAULTLIB:msvcrt.lib") line. I had to link against msvcrt default lib. Why was this there? Is there a better version to link against?
  3. I recompiled VTK 5.04 and made some slight edits to the source so that everything compiled (most notably explicitly casting things that were not explicitly cast in vtkIO). Most of the tests run so I assume that it is pretty much okay but it is still throwing a lot of type cast errors.

[edit] MESH

Windows 2000, XP Before using the mesh creation utilities you will need certain Windows libraries. These are often provided by .NET or Visual Studio. The libraries are

These libraries are usually installed (in C:\Windows\System32, but this may vary across systems). These will be installed automatically if you have the .net framework on your machine, but otherwise you can search for them online. (We've found the site to be secure in providing these files.)

You may prefer to simply install the .NET framework and not worry about the individual files. In that case you can download the .NET framework from the Microsoft download site.

If you try to build a mesh and you receive the error message

> Building mesh...??? Invalid MEX-file
> ' ...\matlab\VISTASOFT\Anatomy\mrMesh\build_mesh.dll':
> specified module was not found.

go install the files as per the instructions above.

In some computers (like KA's Panasonic laptop), the installation of these three dlls doesn't solve the mex error. This might be because a little bit different version of the dll (e.g. msvcp71.dll) is called. You can find out the dependencies of dlls by using a free software named "Dependency Walker" (

Linux, Mac mrMesh runs on 32-bit linux machines and Macs with PowerPC architecture. Support for the newer Intel Macs is more limited: you can build and smooth surface meshes, but not visualize them (the VTK Mesh Server needs to be recompiled for this architecture). For 64-bit linux, see the Known Issues section below.

Known Issues The mrMesh code is known to have problems with 64-bit Intel architecture (specifically, Xeon architecture) machines. The code seems to have no problems with 64-bit AMD processor machines. This is an odd bug which we are trying to figure out; likely the core mrMesh code will be re-implemented in Python to be more portable. This can be worked around (in linux, at least) by starting MATALB with the -glnx86 flag, which forces 32-bit mode.

DY made mrMesh 'work' on her (Windows, AMD) 64-bit machine by: (1) installing the necessary .dlls, (2) double-clicking mrMeshSrv.exe -- found in /VISTASOFT/Anatomy/mrMesh/, (3) THEN using mrMesh commands in mrVista and mrDiffusion.

[edit] Compile mrMeshSrv

Please go to Mesh Compile to download the installer shell script.

Basically, here's what we used to do. Check out the code:

svn co

Then compile it. You might need to edit the Make file and install some extra libraries (like VTK and wxWidgets). On Ubuntu 10.4, try this:

sudo apt-get install libvtk5-dev libwxgtk2.8-dev
cd mrMesh/server

If you are on a 64-bit system, the resulting executable will be called mrMeshSrv.glxa64. On 32-bit it is mrMeshSrv.glx. The instructions above will produce an executable that depends on the dynamically-linked VTK and wxWidgets libraries, so you will need to make sure that these libraries are installed on all the machines that run the mrMeshSrv executable (e.g., sudo apt-get install libvtk5 libwxgtk2.8).

An example of a colleague who successfully downloaded for Ubuntu is:

> On our Ubuntu 9.04 server, i downloaded the files with svn command as 
> indicated on the additional text you put on your wiki. Then i 
> installed missing libraries (basically the two -dev ones).
> I just had to change a path in the Makefile ('vtk-5.2' into 'vtk-5.0') 
> and i compiled the 64-bits version of the mrMsehSrv. We tested it and it worked!

[edit] Compile BuildMesh

Please go to Mesh Compile to download the installer shell script.

[edit] For More Matlab Information

The MathWorks website has a helpful section on MEX-files:

It also documents the use of external interfaces, libraries and so forth:

[edit] Multiple software versions?

If you are working from a linux box, you may be accessing a different repository from the one you intend to. You can check which repository you are accessing by running which -all filename. Make sure that the top-level repository is updated, as desired.

[edit] SPM

Several mrDiffusion functions call on SPM. You can get re-compiled SPM mex files for 64 bit windows in a variety of places including:

There will be a lot of .mexw64 files. If you have a local copy of SPM, go into the SPM directory, and copy the .mexw64 files into the appropriate subdirectories. When I did this in August 2007, I needed to copy over about 30 odd files to the main SPM2 directory, and another 5 files to the toolbox/Deformations subdirectory.

[edit] General

[edit] You try to execute a binary on Linux, e.g., Quench, ConTrack, MicroTrack, and the system complains about a missing shared object file


/contrack_gen.glxa64:/usr/lib64/ version `GLIBCXX_3.4.9' not found (required by contrack_gen.glxa64)

Explanation: You are most likely trying to execute a binary that was compiled with an incompatible toolchain for your Linux distribution.

Solutions: 1. If you are using a Wandell lab machine, compare your Linux distribution with azure. azure is the target for linux compilation and one easy fix is to make sure your machine's Linux distribution is at least the same version or newer than azure's. 2. In general, if you compile the source code on your platform with your own compilation tools, e.g., GNU's gcc set, than you will have a binary that is compatible with your system.

[edit] Host key verification failure

Sometimes, the remote host identification key will change (e.g., firewall settings changed). If this happens, trying to ssh onto a server will result in the following error display:

 dnab422574:~ you$ ssh you@teal
 Someone could be eavesdropping on you right now (man-in-the-middle attack)!
 It is also possible that the RSA host key has just been changed.
 The fingerprint for the RSA key sent by the remote host is
 Please contact your system administrator.
 Add correct host key in /Users/you/.ssh/known_hosts to get rid of this  message.
 Offending key in /Users/you/.ssh/known_hosts:10
 RSA host key for teal has changed and you have requested strict checking.
 Host key verification failed.

If this happens, you will need to fix the relevant line (in this example line 10) of the known_hosts file on each of the computers you ssh from. This file is often hidden: the parent directory starts with a period (".ssh/known_hosts"); in the command line, ln -a will show the directory. An example of how to invoke a simple text editor to edit the file is below:

  cd /Users/you/.ssh
  nano known_hosts

Go into the text file and delete the entire line(s) that relate to the server you are trying to ssh into. Make sure you are accessing the copy of this file on the correct location (your computer), as there are identically-named, but distinct copies of this file on different servers, etc.

Personal tools