Skip to content

Commit 89a971f

Browse files
committed
Update MEX compilation section
1 parent 4b5c672 commit 89a971f

File tree

5 files changed

+110
-96
lines changed

5 files changed

+110
-96
lines changed
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
# Compilation of SPM MEX files
22

3-
Pre-compiled MEX files are provided with the SPM distribution so you shouldn't need to compile the MEX files yourself. We still provide here platform-specific instructions to compile all the MEX files for your machine.
3+
!!! info "What are MEX files?"
4+
5+
MEX files are MATLAB functions implemented in C/C++. Contrary to functions implemented in pure MATLAB, they need to be compiled in order for the MATLAB interpreter to be able to call them. Once compiled, they correspond to platform-specific files `*.mex*`.
6+
7+
## Instructions
8+
9+
Pre-compiled MEX files are provided with the SPM distribution so you shouldn't need to compile the MEX files yourself. We still provide here platform-specific instructions to compile all the MEX files for your machine if the shipped ones were incompatible with your OS version.
410

511
* [Windows](windows.md)
612
* [Linux](linux.md)
713
* [macOS](macos.md)
814

915
## Troubleshooting
1016

17+
Platform-specific throubleshooting is available with the links above. We list here error messages and solutions that are common across platforms.
18+
1119
!!! failure "`bad image handle dimensions` error"
1220

1321
The error "`bad image handle dimensions`" when trying to display an image is an indication that the MEX files are not compatible with your platform and should be recompiled.

docs/development/compilation/linux.md

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Compilation of SPM on Linux
22

3+
This page describes the compilation of SPM MEX files on Linux. It requires the installation of a C/C++ compiler and related tools (e.g. `make`). This can be done on Debian/Ubuntu by installing the `build-essential` package (`sudo apt install build-essential`).
4+
5+
## Instructions
6+
37
In a Terminal, ==from the `src` folder== of your SPM installation, type:
48

59
```
@@ -9,13 +13,15 @@ make external-distclean
913
make external && make external-install
1014
```
1115

16+
This will generate `*.mexa64` MEX files.
17+
1218
## Troubleshooting
1319

1420
!!! failure "`cannot find -lstdc++` error"
1521

16-
If you get the error message "cannot find -lstdc++", try installing the build-essential package using your package manager (i.e. sudo apt-get install build-essential on Ubuntu), or the specific libstdc++5 package (replacing 5 with the version of your system) with sudo apt-get install libstdc++5.
22+
If you get the error message "`cannot find -lstdc++`", try installing the `build-essential` package using your package manager (i.e. `sudo apt install build-essential` on Ubuntu), or the specific `libstdc++5` package (replacing 5 with the version of your system) with `sudo apt-get install libstdc++5`.
1723

18-
If, after the install of libstdc++5, the lstdc++ error persists, just add a symbolic link with:
24+
If, after the install of `libstdc++5`, the `lstdc++` error persists, just add a symbolic link with:
1925

2026
```
2127
sudo ln -s /usr/lib64/libstdc++.so.6 /usr/lib64/libstdc++.so
@@ -25,28 +31,21 @@ make external && make external-install
2531

2632
For old versions of Ubuntu and MATLAB, have a look at [these instructions](https://web.archive.org/web/20160307232613/http://www.lukedodd.com/compiling-gcc-4-3-4-under-ubuntu-11-10-for-matlab/).
2733

28-
!!! failure "Other problems with lstdc++"
29-
30-
If you get the above error and cannot install libstdc++ (e.g. you do not have root/admin access), or if you get other problems such as "... version `GCC_4.2.0' not found (required by /usr/lib64/libstdc++.so.6", then another option is to edit your mexopts.sh config file and delete or disable the -lstdc++ compiler flags, for example simply use find-and-replace to delete all instances of -lstdc++. SPM will compile properly without this.
34+
!!! failure "Other problems with `lstdc++`"
3135

32-
It's best to edit a local copy of mexopts.sh (the original is found in the bin directory with your matlab and mex executables). If you run `mex -setup` it will interactively create a copy for you, under `~/.matlab/RELEASE`, for example:
36+
If you get the above error and cannot install `libstdc++` (e.g. you do not have root/admin access), or if you get other problems such as "`... version `GCC_4.2.0' not found (required by /usr/lib64/libstdc++.so.6`", then another option is to edit your `mexopts.sh` config file and delete or disable the `-lstdc++` compiler flags, for example simply use find-and-replace to delete all instances of `-lstdc++`. SPM will compile properly without this.
3337

34-
```
35-
~/.matlab/R14SP3/mexopts.sh
36-
~/.matlab/R2008b/mexopts.sh
37-
```
38+
It's best to edit a local copy of `mexopts.sh` (the original is found in the `bin` directory with the `matlab` and `mex` executables). If you run `mex -setup` it will interactively create a copy for you, under `~/.matlab/RELEASE`, for example:
3839

39-
or similar, and you may then further edit that copy if required. If you match the release, mex will find your copy of `mexopts.sh`. Note that MATLAB
40-
4140
```
42-
version
41+
~/.matlab/R2020a/mexopts.sh
4342
```
4443

45-
should show you the release, but with parentheses around it, e.g. `"(R14SP3)"`.
44+
or similar, and you may then further edit that copy if required. If you match the release, `mex` will find your copy of `mexopts.sh`.
4645

4746
!!! failure "`mex: command not found` error"
4847

49-
If you get the error "mex: command not found" check that mex is in your path. Either add the MATLAB binary directory (usually `/usr/local/matlab/bin`) to your path or create a link to mex somewhere already in the path (usually `/usr/local/bin)`.
48+
If you get the error "`mex: command not found`" check that `mex` is in your path. Either add the MATLAB binary directory (usually `/usr/local/matlab/bin`) to your path or create a link to `mex` somewhere already in the path (usually `/usr/local/bin)`.
5049

5150
Adding the path:
5251
```
@@ -59,10 +58,10 @@ make external && make external-install
5958
ln -s /usr/local/matlab/bin/mex
6059
```
6160

62-
Another solution is to edit the Makefile (`spm8/src/Makefile.var` for SPM8 or `spm5/src/Makefile` for SPM5) and mention the full path when referring to MEXBIN:
61+
Another solution is to provide the full path to the `mex` executable in an environment variable called `MEXBIN`. It will be used by the `Makefile` instead of the default one.
6362

6463
```
65-
MEXBIN = /usr/local/matlab/mex
64+
MEXBIN=/usr/local/matlab/bin/mex make
6665
```
6766

6867
!!! failure "`This is pdfTeX, Version ... (TeX Live 2011)` error"
@@ -74,28 +73,30 @@ make external && make external-install
7473
This is pdfTeX, Version ... (TeX Live 2011)
7574
```
7675

77-
This is due to a conflict between MATLAB mex and a LaTeX command (from the texlive package) with the same name. Change `PATH` or `MEXBIN` as described in the previous solution.
76+
This is due to a conflict between MATLAB `mex` and a LaTeX command (from the texlive package) with the same name. Change `PATH` or `MEXBIN` as described in the previous solution to make sure that the MATLAB `mex` executable is called first.
7877

7978
!!! failure "Missing files"
8079

8180
If you get build errors such as "`math.h` does not exist" when trying to execute the command `make && make install`, then you probably need to install the `build-essentials` package.
8281

83-
At the command line type:
82+
At the command line in a Terminal, type:
8483
```
8584
sudo apt install build-essential
8685
```
8786

87+
or similar command to install a C/C++ compilation environment if you are not using a Debian-based Linux distribution.
88+
8889
!!! failure "`plugin needed to handle lto object` error"
8990

90-
Edit `spm12/src/Makefile.var` at the line defining `AR` so that it reads:
91+
Edit `spm/src/Makefile.var` at the line defining `AR` so that it reads:
9192

9293
```
9394
AR = gcc-ar rcs
9495
```
9596

9697
!!! failure "`Crash at startup`"
9798

98-
The following concerns a situation where MATLAB segfaults when opening the SPM interface with errors like:
99+
The following concerns a situation where MATLAB generates a segmentation fault when opening the SPM interface with errors like:
99100
```
100101
BadWindow (invalid Window parameter)
101102
```
Lines changed: 66 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,101 @@
11
# Compilation of SPM on macOS
22

3+
This page describes the compilation of SPM MEX files on macOS.
4+
35
!!! tip
4-
If you are using macOS Catalina, Big Sur, Monterey or Ventura, check the [Troubleshooting section](#troubleshooting) first as you should not need to recompile the MEX files.
6+
If you are using macOS Catalina, Big Sur, Monterey or Ventura and have apparent problems with the MEX files provided with SPM, check the [Troubleshooting section](#troubleshooting) first as it is very likely you do not need to recompile the MEX files.
57

6-
Should you want to compile SPM MEX files, you need to have Apple's
7-
development environment [Xcode](http://developer.apple.com/tools/xcode/)
8-
installed.
8+
To compile SPM MEX files, you need to have Apple's development environment [Xcode](http://developer.apple.com/tools/xcode/) installed.
99

10-
You also need to have the *mex* executable in your system path. To do
10+
You also need to have the `mex` executable in your system path. To do
1111
so, type the following in a Terminal:
1212
```
13-
export PATH=/Applications/MATLAB_R2017a.app/bin:\$PATH
13+
export PATH=/Applications/MATLAB_R2017a.app/bin:$PATH
1414
```
1515
with the appropriate path where MATLAB is installed
1616

17-
Then, in a Terminal, from the *src* folder of your SPM12 installation,
17+
If you get errors such as `Bad : modifier in $ (/)`, this is because
18+
the instructions are given for a `bash` Terminal while you are using a
19+
`tcsh` Terminal. The equivalent commands are:
20+
```
21+
setenv PATH /Applications/MATLAB_R2017a.app/bin:${PATH}
22+
```
23+
24+
## Instructions
25+
26+
In a Terminal, from the *src* folder of your SPM installation,
1827
type:
1928
```
20-
cd /Users/login/spm12/src
29+
cd /Users/login/spm/src
2130
make distclean
2231
make && make install
2332
make external-distclean
2433
make external && make external-install
2534
```
2635

27-
If you get errors such as `Bad : modifier in \$ (/)`, this is because
28-
the instructions are given for a bash` Terminal while you are using a
29-
`tcsh` Terminal. The equivalent commands are:
30-
```
31-
setenv PATH /Applications/MATLAB_R2017a.app/bin:\${PATH}
32-
```
33-
34-
If you get errors such as `xcrun: error: SDK "macosx10.14.1" cannot be
35-
located` while compiling, execute the following:
36-
```
37-
sudo xcode-select --switch /Applications/Xcode.app/
38-
```
36+
This will generate `*.mexmaci64` or `*.mexmaca64` MEX files.
3937

4038
## Troubleshooting
4139

42-
### macOS Catalina, Big Sur, Monterey, Ventura
43-
44-
If you have issues with MEX files on macOS with one of these errors:
40+
!!! failure "macOS Catalina, Big Sur, Monterey, Ventura"
4541

46-
```
47-
"*.mexmaci64" cannot be opened because the developer cannot be verified.
48-
macOS cannot verify that this app is free from malware
49-
```
50-
```
51-
Code signature not valid for use in process using Library Validation: library load disallowed by system policy
52-
```
53-
open a Terminal, `cd` to the SPM directory and type:
42+
If you have issues with MEX files on macOS with one of these errors:
5443

55-
```
56-
find . -name "*.mexmaci64" -exec xattr -d com.apple.quarantine {} \;
57-
```
44+
```
45+
"*.mexmaci64" cannot be opened because the developer cannot be verified.
46+
macOS cannot verify that this app is free from malware
47+
```
48+
```
49+
Code signature not valid for use in process using Library Validation: library load disallowed by system policy
50+
```
51+
open a Terminal, `cd` to the SPM directory and type:
5852

59-
If it doesn't work, please try this equivalent alternative, replacing
60-
`SPM_PATH` with the path of your SPM installation:
53+
```
54+
find . -name "*.mexmaci64" -exec xattr -d com.apple.quarantine {} \;
55+
```
6156

62-
```
63-
sudo xattr -r -d com.apple.quarantine SPM_PATH
64-
sudo find SPM_PATH -name "*.mexmaci64" -exec spctl --add {} \;
65-
```
66-
67-
### Java Exception and abrupt exit on Mac OS X version 10.10 Yosemite
57+
If it doesn't work, please try this equivalent alternative, replacing `SPM_PATH` with the path of your SPM installation:
6858

69-
If you get MATLAB issues with Yosemite, see the following bug report for
70-
a patch:
59+
```
60+
sudo xattr -r -d com.apple.quarantine SPM_PATH
61+
sudo find SPM_PATH -name "*.mexmaci64" -exec spctl --add {} \;
62+
```
7163

72-
[`http://www.mathworks.com/support/bugreports/1098655`](http://www.mathworks.com/support/bugreports/1098655)
64+
!!! failure "`xcrun: error: SDK "macosx10.14.1" cannot be located` error"
7365

74-
### "`This is pdfTeX, Version ...`" error
66+
If you get errors such as `xcrun: error: SDK "macosx10.14.1" cannot be located` while compiling, execute the following before compilation:
67+
```
68+
sudo xcode-select --switch /Applications/Xcode.app/
69+
```
7570

76-
If you get an error message such as:
71+
!!! failure "Java Exception and abrupt exit on Mac OS X version 10.10 Yosemite"
7772

78-
```
79-
mex -O -c spm_vol_utils.c -DSPM_UNSIGNED_CHAR
80-
mex: unrecognized option `-O'
81-
mex: unrecognized option `-c'
82-
This is pdfTeX, Version 3.1415926-2.3-1.40.12 (TeX Live 2011)
83-
restricted \write18 enabled.
84-
entering extended mode
85-
(./spm_vol_utils.c
86-
This is MeX Version 1.05 18 XII 1993 (B. Jackowski & M. Ry\'cko)
87-
! You can't use `macro parameter character #' in vertical mode.
88-
```
73+
If you get MATLAB issues with Yosemite, see the following bug report for a patch:
8974

90-
this is due to a conflict between MATLAB `mex` and a LaTeX command with
91-
the same name. Edit `src/Makefile.var` and mention the full path when
92-
referring to MEXBIN
75+
[`http://www.mathworks.com/support/bugreports/1098655`](http://www.mathworks.com/support/bugreports/1098655)
9376

94-
```
95-
MEXBIN = /Applications/MATLAB_R2012a.app/bin/mex
96-
```
77+
!!! failure "`This is pdfTeX, Version ...` error"
9778

98-
or change PATH accordingly.
79+
If you get an error message such as:
80+
```
81+
mex -O -c spm_vol_utils.c -DSPM_UNSIGNED_CHAR
82+
mex: unrecognized option `-O'
83+
mex: unrecognized option `-c'
84+
This is pdfTeX, Version 3.1415926-2.3-1.40.12 (TeX Live 2011)
85+
restricted \write18 enabled.
86+
entering extended mode
87+
(./spm_vol_utils.c
88+
This is MeX Version 1.05 18 XII 1993 (B. Jackowski & M. Ry\'cko)
89+
! You can't use `macro parameter character #' in vertical mode.
90+
```
91+
this is due to a conflict between MATLAB `mex` and a LaTeX command with
92+
the same name. Edit `src/Makefile.var` and mention the full path when
93+
referring to MEXBIN
94+
```
95+
MEXBIN = /Applications/MATLAB_R2012a.app/bin/mex
96+
```
97+
or change PATH accordingly.
9998

100-
### Slowdown when using graphical inputs
99+
!!! failure "Slowdown when using graphical inputs"
101100

102-
This seems to happen when a window manager for macOS is installed ([Magnet](https://magnet.crowdcafe.com/) or [Tiles](https://freemacsoft.net/tiles/)), see also
103-
[this](https://www.mathworks.com/matlabcentral/answers/475682).
101+
This seems to happen when a window manager for macOS is installed ([Magnet](https://magnet.crowdcafe.com/) or [Tiles](https://freemacsoft.net/tiles/)), see also [this](https://www.mathworks.com/matlabcentral/answers/475682).

docs/development/compilation/windows.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Compilation of SPM on Windows
22

3-
This page describes the compilation of SPM12 MEX files on **Windows 10**
4-
with **MATLAB R2018b (9.5)** and **MSYS2 MinGW-w64 GCC**. It also provides instructions to compile SPM for the Octave package of MSYS2.
3+
This page describes the compilation of SPM MEX files on **Windows 10**
4+
with **MATLAB R2018b (9.5)** and **MSYS2 MinGW-w64 GCC**; identical or very similar instructions should work with newer versions of Windows, MATLAB and MSYS2 MinGW. It also provides instructions to compile SPM for the Octave package of MSYS2.
55

66
!!! tip
77
If you use other versions of MATLAB and/or compiler, make sure that they are compatible by checking the list of [supported compilers](https://www.mathworks.com/support/requirements/previous-releases.html) and adjust the MATLAB path accordingly in the commands below.
@@ -28,18 +28,20 @@ mex -setup
2828
% MEX configured to use 'MinGW64 Compiler (C)' for C language compilation.
2929
```
3030

31-
Do not worry if there is a warning:
31+
Do not worry if this warning is displayed:
3232

3333
```
3434
Warning: The MATLAB C and Fortran API has changed to support MATLAB
35+
variables with more than 2^32-1 elements. You will be required
36+
to update your code to utilize the new API.
3537
```
3638

3739
## Compilation
3840

39-
In MSYS2, move to the SPM12 source directory with
41+
In MSYS2, move to the SPM source directory (`./src/`) with
4042

4143
```
42-
cd  /c/Documents and Settings/`*`login`*`/Documents/MATLAB/spm12/src
44+
cd  /c/Documents and Settings/<login>/Documents/MATLAB/spm/src
4345
```
4446

4547
then set the PATH appropriately
@@ -59,6 +61,8 @@ make external-distclean
5961
make external && make external-install
6062
```
6163

64+
This will generate `*.mexw64` MEX files.
65+
6266
## Notes for compilation with MSYS2 for Octave
6367

6468
Install
@@ -77,9 +81,11 @@ then start Octave with:
7781
and compile with:
7882

7983
```bash
80-
cd('/c/Documents and Settings/<login>/Documents/MATLAB/spm12/src');
84+
cd('/c/Documents and Settings/<login>/Documents/MATLAB/spm/src');
8185
system('make distclean PLATFORM=octave');
8286
system('make PLATFORM=octave && make install PLATFORM=octave');
8387
system('make external-distclean PLATFORM=octave');
8488
system('make external PLATFORM=octave && make external-install PLATFORM=octave');
8589
```
90+
91+
This will generate `*.mex` MEX files.

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ nav:
128128
- 'Git': 'development/git.md'
129129
- 'Guidelines': 'development/guidelines.md'
130130
- 'Testing': 'development/testing.md'
131+
- 'Compilation': 'development/compilation/index.md'
131132

132133
markdown_extensions:
133134
- abbr

0 commit comments

Comments
 (0)