bugGNU Octave - Bugs: bug #54804, mex does not support setting...


bug #54804: mex does not support setting environment variables on command line

Submitted by:  Richard <crobar>
Submitted on:  Mon 08 Oct 2018 09:50:22 AM UTC  
Category:  Octave Function Severity:  1 - Wish
Priority:  3 - Low Item Group:  Feature Request
Status:  Need Info Assigned to:  None
Originator Name:  Richard Crozier Open/Closed:  Open
Release:  4.4.1 Operating System:  Any

Add a New Comment(Rich Markup)

You are not logged in

Please log in, so followups can be emailed to you.


Wed 10 Oct 2018 01:07:11 PM UTC, comment #3:

Oh, and regarding the linux command line version, yes, I believe it is the same for this, at least the help for that script indicates that this is the case as it also contains the varname=varvalue option description.

Richard <crobar>
Wed 10 Oct 2018 01:04:11 PM UTC, comment #2:

In matlab, for each supported compiler there is an xml file (since recent versions, previously this was done with a shell script or a windows batch file) which defines various settings for that compiler for any languages it supports. It is there that the variables which may be redefined in this way are initially defined. The mex -setup command basically selects one of these XML config files to allow switching between different compiler configurations. I'm not suggesting Octave needs to go this far, but making some variables able to be overridden in this way is helpful. I agree that it would be nice if mkoctfile supported a similar syntax.

I have struggled to find any official documentation on the xml file format, although I did find a githup repo with examples someone has created here:


Richard <crobar>
Tue 09 Oct 2018 10:47:03 PM UTC, comment #1:

This does seem like an interesting and useful feature to integrate into the compiler wrapper. Maybe we want to include the same syntax in mkoctfile as well.

Does this apply to calling the mex program directly as well? It does seem to if I read the Matlab help page correctly. This means it's not just a feature of the mex.m wrapper function, it needs to be supported in the mkoctfile program itself.

Is there a discrete list of environment variables that can be passed this way? Does it accept any argument that looks like an environment variable? Does it have to be all uppercase or will it accept 'cflags=-g' for example? Will it accept 'OCTAVE=yes'? Can you think of a way to test whether that sets a custom environment variables?

The other interesting bit is that it looks like mex parses the value of the environment variable and does parameter expansion itself. The docs say to use single quotes, for example CXXFLAGS='$CXXFLAGS -Wall' as you showed. The single quotes means that the shell isn't expanding that, the mex program must be doing that expansion itself.

Mike Miller <mtmiller>
Project Administrator
Mon 08 Oct 2018 09:50:22 AM UTC, original submission:

Matlab's implementation of mex supports setting compiler flags and many other things by means of key-value pair arguments in the call to mex. This is not supported by Octave's mex.m or mkoctfile.

On the main documentation for the mex function it states:


Overrides default setting for variable varname. This option is processed after all command-line arguments are considered.

For more detailed documentation on this, see


so, for example one can do:

mex -v CXXFLAGS='$CXXFLAGS -Wall' yprime.c

to append -Wall to the existing CXXFLAGS

The flags which can be modified include the usual culprits, CFLAGS, CXXFLAGS, LDFLAGS, CC, LD, CXX etc, but depend on what is being compiled. For example, for a C source file, the verbose output of mex produces the following (in R2018a):

Basically every variable here can be modified using the key-value pairs arguments, and it is really very useful to be able to do this.

for a C++ source file I get the following

Richard <crobar>


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

No files currently attached


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by crobar (Submitted the item)

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.




    Follow 5 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2018-10-09 mtmiller Severity3 - Normal => 1 - Wish
        Priority5 - Normal => 3 - Low
        Item GroupMatlab Compatibility => Feature Request
        StatusNone => Need Info
        Summarymex.m does not support setting flags in the same manner as the Matlab implementation => mex does not support setting environment variables on command line

    Back to the top

    Powered by Savane 3.3