Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Problem in specifying dependent library files by using MEX switches

Asked by AP

AP (view profile)

on 13 Feb 2013

I am trying to find the instructions for specifying the dependent library files as well as the path for compiling a c++ MEX file. Unfortunately, I wasn't successful and I couldn't get it to work. I don't know how to use:

 COMPFLAGS -I   -l

Could someone please instruct me how to use these switches in Windows for the following case?

 /LIBPATH:"C:\Program Files (x86)\lib\" / l64.lib cr.lib al.lib 

Thanks,

Ahmad

0 Comments

AP

AP (view profile)

Products

No products are associated with this question.

2 Answers

Answer by Kaustubha Govind

Kaustubha Govind (view profile)

on 13 Feb 2013

You should just need to add them on to the "mex" command. Probably something like:

    >> mex myfile.c -L"C:\Program Files (x86)\lib\" -l l64.lib -l cr.lib -l al.lib 

3 Comments

AP

AP (view profile)

on 13 Feb 2013

Thanks Kaustubha. I also added the path of include files to the MEX command but unfortunately, it still does not work. I also put a spaced after -I and -L but no success.

 >> mex myMEX.cpp -I"C:\Program Files (x86)\FLRecon\Include\" -L"C:\Program Files (x86)\FLRecon\lib\" -l l64.lib -l cr.lib -l al.lib

I get the following error:

 cl : Command line warning D9024 : unrecognized source file type 'Files\MATLAB\R2012b\extern\include -IC:\Program', object file assumed 
DNDEBUG -DMX_COMPAT_32 myMEX.cpp 
c1xx : fatal error C1083: Cannot open source file: 'Files\MATLAB\R2012b\simulink\include /O2 /Oy- /DNDEBUG -DMX_COMPAT_32 myMEX.cpp': No such file or directory 
C:\PROGRA~1\MATLAB\R2012B\BIN\MEX.PL: Error: Compile of 'myMEX.cpp' failed. 
James Tursa

James Tursa (view profile)

on 13 Feb 2013

Sometimes the mex command gets confused with spaces imbedded in strings when using command line syntax. I would try the function syntax instead. E.g. something like:

mex('myMEX.cpp','-I','"C:\Program Files (x86)\FLRecon\Include\"','-L','"C:\Program Files (x86)\FLRecon\lib\"','-l','l64.lib','-l','cr.lib','-l','al.lib')
AP

AP (view profile)

on 13 Feb 2013

Thanks James. Your comment 100% makes sense to me. However, I get the following error:

 C:\PROGRA~1\MATLAB\R2012B\BIN\MEX.PL: Error: 'C:\Program' not found. 
Kaustubha Govind

Kaustubha Govind (view profile)

Answer by Jan Simon

Jan Simon (view profile)

on 13 Feb 2013
Edited by Jan Simon

Jan Simon (view profile)

on 13 Feb 2013

I had a similar problem with a space in the CFLAGS for Linux: Too add 'CFLAGS="\$CFLAGS -std=c99"' Matlab needs:

mex('CFLAGS="\$CFLAGS', '-std=c99"', ...)

The MEX command is too smart for parsing the non-functional form, so try this:

mex('myfile.c', '-L"C:\Program', 'Files', '(x86)\lib\"',  '-l l64.lib', ...
    '-l cr.lib', '-l al.lib')

This looks as strange as it is. Perhaps the spaces after "-l" are a problem also. I think they should be omitted.

I'm convinced, that MEX has been grown beyond a fair level of usability: Under Windows mex -setup creates a .bat file, which is not called as batch file, but parsed and evaluated in a perl script. Actually starting a compiler is trivial and a handmade M-function would be cleaner and more stable.

2 Comments

AP

AP (view profile)

on 13 Feb 2013

Jan, I tried your solution as following:

 mex('myMEX.cpp','-I"C:\Program','Files','(x86)\FLRecon\include\"','-L"C:\Program','Files','(x86)\FLRecon\lib"','-l lp64.lib','-l cr.lib','-l al.lib')

This is what MATLAB complains about:

 cl : Command line warning D9024 : unrecognized source file type 'Files\MATLAB\R2012b\extern\include -IC:\Program', object file assumed 
DNDEBUG -DMX_COMPAT_32 myMEX.cpp 
c1xx : fatal error C1083: Cannot open source file: 'Files\MATLAB\R2012b\simulink\include /O2 /Oy- /DNDEBUG -DMX_COMPAT_32 myMEX.cpp': No such file or directory 
C:\PROGRA~1\MATLAB\R2012B\BIN\MEX.PL: Error: Compile of 'myMEX.cpp' failed. 
 Error using mex (line 206)
Unable to complete successfully.
Kaustubha Govind

Kaustubha Govind (view profile)

on 14 Feb 2013

What about:

mex('myMEX.cpp','-I"C:\Program','Files','(x86)\FLRecon\include\"','-L"C:\Program Files (x86)\FLRecon\lib"','-l lp64.lib','-l cr.lib','-l al.lib')

(I'm wondering if maybe the -L option knows how to deal with the double-quote)

Jan Simon

Jan Simon (view profile)

Contact us