C++ comments in Mex files under Linux

2 views (last 30 days)
For many years users support problems about compiling C-sources containing C++ comments under Linux, because the automatically created mexopts file enables the -ansi style instead of the modern std=-c99 style for the GCC compiler.
Other compilers accept the C++ comments for a long time: LCC (shipped with Matlab or downloaded from the net), Borland BCC, OpenWatcom, MSVC, Intel ICC, Tiny C (TCC), PCC, ...
Is there any reason not to enable the 12 year old C99 standard in the default mexopts file for the GCC under Linux?
BTW: To solve the problem, you can edit the mexopts file: replace '-ansi' by '-std=c99' or add these flags to the MEX call:
mex -O CFLAGS="\$CFLAGS -std=c99" yourfile.c
  1 Comment
Matt
Matt on 3 Apr 2013
Edited: Walter Roberson on 4 Apr 2013
I have a follow-up question... I am using Simulink s-functions that have C++ style comments. I have changed the CFLAGS option in the mexopts file and can mex the S-functions no problem. However, when I run the model in rapid accelerator mode, it appears to recompile the s-functions and uses -ansi instead of -std=c99.
Is there a recommended way to address this problem? I have found a solution, but it involves manually changing the MODEL.mk file in the slprj folder or actually changing the default raccel tmf file, both of which seem sketchy to me.
EDIT: More info... I traced it back to unixtools.mk, which changes the CFLAGS based on the value of TGT_FCN_LIB in the makefile. Apparently, TGT_FCN_LIB takes the value of ANSI_C, ISO_C, or GNU and the CFLAGS change accordingly. TGT_FCN_LIB is set in the MODEL.mk makefile, but I am not sure what parameter in the model's configuration parameters determines its value (if any). I manually changed it to ISO_C in MODEL.mk and everything compiles fine.

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 6 Jun 2011
Probably it's the same reason they still include the silly "/fixed" option on the Fortran mexopts.bat files (which forces free-format source code to be interpreted by the compiler as fixed-format source code). It doesn't affect their sample code and they haven't had enough user complaints to motivate them to change it (which would require them to re-validate all of their sample code I presume).
  2 Comments
James Tursa
James Tursa on 6 Jun 2011
BTW, first thing I do after installing a new MATLAB version is to go change the mexopts.bat files to remove the "/fixed" option from the COMPFLAGS line.
Jan
Jan on 6 Jun 2011
Then I have to forward all complaints from Linux users concerning the C99 comments in my numerous C-Mex FEX submissions to the TMW staff.

Sign in to comment.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!