Thread Subject: Compiling 64-bit mex file on MacOS X

Subject: Compiling 64-bit mex file on MacOS X

From: Alan

Date: 7 Apr, 2011 15:27:05

Message: 1 of 7

I am running MATLAB 2010b on MacOS 10.6.7. I wrote a simple mex file. When compiling it using mex command, I got the following error:

$ /Applications/MATLAB_R2010b.app/bin/mex advxmx.F
ld: warning: in /sw/lib/gcc4.5/lib/libgfortran.dylib, file was built for i386 which is not the architecture being linked (x86_64)
ld: warning: in /sw/lib/gcc4.5/lib/gcc/i386-apple-darwin10.6.0/4.5.0/libgfortranbegin.a, file was built for unsupported file format which is not the architecture being linked (x86_64)
ld: warning: in /sw/lib/gcc4.5/lib/gcc/i386-apple-darwin10.6.0/4.5.0/libgcc.a, file was built for unsupported file format which is not the architecture being linked (x86_64)
Undefined symbols:
  "_advx_", referenced from:
      _mexfunction_ in advxmx.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

    mex: link of ' "advxmx.mexmaci64"' failed.

My 'computer' says MACI64. And I listed this:
$ ls /sw/lib/gcc4.5/lib/gcc/i386-apple-darwin10.6.0/4.5.0/
crt3.o crtprec64.o include/ install-tools/ libgcov.a plugin/
crtfastmath.o crtprec80.o include-fixed/ libgcc.a libgfortranbegin.a x86_64/
crtprec32.o finclude/ include-gnu-runtime/ libgcc_eh.a libgfortranbegin.la

Somehow the mex wants to link to library files in this directory, not the 'x86_64' directory inside it.

This happens to me with both MATLAB 2010a and 2010b, and on both an iMac and a MB Air. The gcc was installed using fink. My XCode version is 3.2.5 64-bit on one and 3.2.6 64-bit on another.

I also noticed that in the mexopts.sh file under my .matlab directory, in the maci64 section I have this:
SDKROOT='/Developer/SDKs/MacOSX10.5.sdk'
I am wondering why this root is not SDKROOT='/Developer/SDKs/MacOSX10.6.sdk' even though I already have both MacOSX10.5.sdk and MacOSX10.6.sdk under /Developer/SDKs/. This mexopts.sh was generated by mex -setup and is not changed.

Subject: Compiling 64-bit mex file on MacOS X

From: Brian Arnold

Date: 7 Apr, 2011 18:43:19

Message: 2 of 7

Hi Alan,

The problem is with the architecture of the gfortran compiler you
installed under /sw/, its libraries are 32-bit (i386). MATLAB needs to
link 64-bit (x86_64). You'll need to find a working 64-bit gfortran
compiler whose libraries are 64-bit and ABI-compatible with Apple's gcc.

If you are building gcc yourself under /sw/ yourself, try rebuilding
with the -m64 option in CFLAGS, CXXFLAGS, and LDFLAGS.

Brian

Alan wrote:
> I am running MATLAB 2010b on MacOS 10.6.7. I wrote a simple mex file.
> When compiling it using mex command, I got the following error:
>
> $ /Applications/MATLAB_R2010b.app/bin/mex advxmx.F ld: warning: in
> /sw/lib/gcc4.5/lib/libgfortran.dylib, file was built for i386 which is
> not the architecture being linked (x86_64)
> ld: warning: in
> /sw/lib/gcc4.5/lib/gcc/i386-apple-darwin10.6.0/4.5.0/libgfortranbegin.a,
> file was built for unsupported file format which is not the architecture
> being linked (x86_64)
> ld: warning: in
> /sw/lib/gcc4.5/lib/gcc/i386-apple-darwin10.6.0/4.5.0/libgcc.a, file was
> built for unsupported file format which is not the architecture being
> linked (x86_64)
> Undefined symbols:
> "_advx_", referenced from:
> _mexfunction_ in advxmx.o
> ld: symbol(s) not found
> collect2: ld returned 1 exit status
>
> mex: link of ' "advxmx.mexmaci64"' failed.
>
> My 'computer' says MACI64. And I listed this:
> $ ls /sw/lib/gcc4.5/lib/gcc/i386-apple-darwin10.6.0/4.5.0/
> crt3.o crtprec64.o include/
> install-tools/ libgcov.a plugin/
> crtfastmath.o crtprec80.o include-fixed/
> libgcc.a libgfortranbegin.a x86_64/
> crtprec32.o finclude/ include-gnu-runtime/
> libgcc_eh.a libgfortranbegin.la
> Somehow the mex wants to link to library files in this directory, not
> the 'x86_64' directory inside it.
>
> This happens to me with both MATLAB 2010a and 2010b, and on both an iMac
> and a MB Air. The gcc was installed using fink. My XCode version is
> 3.2.5 64-bit on one and 3.2.6 64-bit on another.
>
> I also noticed that in the mexopts.sh file under my .matlab directory,
> in the maci64 section I have this:
> SDKROOT='/Developer/SDKs/MacOSX10.5.sdk'
> I am wondering why this root is not
> SDKROOT='/Developer/SDKs/MacOSX10.6.sdk' even though I already have both
> MacOSX10.5.sdk and MacOSX10.6.sdk under /Developer/SDKs/. This
> mexopts.sh was generated by mex -setup and is not changed.

Subject: Compiling 64-bit mex file on MacOS X

From: Alan

Date: 8 Apr, 2011 16:28:05

Message: 3 of 7

Brian,

This for the answer. I downloaded a 64-bit version of gfortran and now the compliation went smoothly. However when I run the program it crashes Matlab. To isolate the problem, I stripped down my program to the minimum that still causing the crash, and here it is:

#include "fintrf.h"
      subroutine mexfunction(nlhs,plhs,nrhs,prhs)

c Required declarations for MATLAB
        mwPointer plhs(*),prhs(*)
        integer nlhs,nrhs
c Prototype the return types of these functions
        mwPointer mxGetPr
        mwSize mxGetM, mxGetN
        real*8 mxGetScalar
        mwPointer mxCreateDoubleMatrix

        integer mxIsDouble, mxIsComplex

c Pointer to classid used to create MATLAB single matrices
        mwPointer classid

c Associate classid with double precision (FORTRAN real*8)
        classid = mxClassIDFromClassName('double')

c Get variables passed from MATLAB, properly typed
        dx = mxGetPr(prhs(1))

c Create MATLAB return matrices
        plhs(1) = mxCreateDoubleMatrix(10,10,classid,0)

        return
        end

With the above program, when I call it inside Matlab with:

dt=0.25;
testmx(dx);


It crashed Matlab. Where is the problem?


~

Brian Arnold <Brian.Arnold@mathworks.com> wrote in message <4D9E05C7.9020702@mathworks.com>...
> Hi Alan,
>
> The problem is with the architecture of the gfortran compiler you
> installed under /sw/, its libraries are 32-bit (i386). MATLAB needs to
> link 64-bit (x86_64). You'll need to find a working 64-bit gfortran
> compiler whose libraries are 64-bit and ABI-compatible with Apple's gcc.
>
> If you are building gcc yourself under /sw/ yourself, try rebuilding
> with the -m64 option in CFLAGS, CXXFLAGS, and LDFLAGS.
>
> Brian
>
> Alan wrote:
> > I am running MATLAB 2010b on MacOS 10.6.7. I wrote a simple mex file.
> > When compiling it using mex command, I got the following error:
> >
> > $ /Applications/MATLAB_R2010b.app/bin/mex advxmx.F ld: warning: in
> > /sw/lib/gcc4.5/lib/libgfortran.dylib, file was built for i386 which is
> > not the architecture being linked (x86_64)
> > ld: warning: in
> > /sw/lib/gcc4.5/lib/gcc/i386-apple-darwin10.6.0/4.5.0/libgfortranbegin.a,
> > file was built for unsupported file format which is not the architecture
> > being linked (x86_64)
> > ld: warning: in
> > /sw/lib/gcc4.5/lib/gcc/i386-apple-darwin10.6.0/4.5.0/libgcc.a, file was
> > built for unsupported file format which is not the architecture being
> > linked (x86_64)
> > Undefined symbols:
> > "_advx_", referenced from:
> > _mexfunction_ in advxmx.o
> > ld: symbol(s) not found
> > collect2: ld returned 1 exit status
> >
> > mex: link of ' "advxmx.mexmaci64"' failed.
> >
> > My 'computer' says MACI64. And I listed this:
> > $ ls /sw/lib/gcc4.5/lib/gcc/i386-apple-darwin10.6.0/4.5.0/
> > crt3.o crtprec64.o include/
> > install-tools/ libgcov.a plugin/
> > crtfastmath.o crtprec80.o include-fixed/
> > libgcc.a libgfortranbegin.a x86_64/
> > crtprec32.o finclude/ include-gnu-runtime/
> > libgcc_eh.a libgfortranbegin.la
> > Somehow the mex wants to link to library files in this directory, not
> > the 'x86_64' directory inside it.
> >
> > This happens to me with both MATLAB 2010a and 2010b, and on both an iMac
> > and a MB Air. The gcc was installed using fink. My XCode version is
> > 3.2.5 64-bit on one and 3.2.6 64-bit on another.
> >
> > I also noticed that in the mexopts.sh file under my .matlab directory,
> > in the maci64 section I have this:
> > SDKROOT='/Developer/SDKs/MacOSX10.5.sdk'
> > I am wondering why this root is not
> > SDKROOT='/Developer/SDKs/MacOSX10.6.sdk' even though I already have both
> > MacOSX10.5.sdk and MacOSX10.6.sdk under /Developer/SDKs/. This
> > mexopts.sh was generated by mex -setup and is not changed.

Subject: Compiling 64-bit mex file on MacOS X

From: Christopher Hulbert

Date: 8 Apr, 2011 16:49:03

Message: 4 of 7

On 4/8/11 12:28 PM, Alan wrote:
> Brian,
>
> This for the answer. I downloaded a 64-bit version of gfortran and now the
> compliation went smoothly. However when I run the program it crashes Matlab. To
> isolate the problem, I stripped down my program to the minimum that still
> causing the crash, and here it is:
>
> #include "fintrf.h"
> subroutine mexfunction(nlhs,plhs,nrhs,prhs)
>
> c Required declarations for MATLAB
> mwPointer plhs(*),prhs(*)
> integer nlhs,nrhs
> c Prototype the return types of these functions
> mwPointer mxGetPr
> mwSize mxGetM, mxGetN
> real*8 mxGetScalar
> mwPointer mxCreateDoubleMatrix
>
> integer mxIsDouble, mxIsComplex
>
> c Pointer to classid used to create MATLAB single matrices
> mwPointer classid
>
> c Associate classid with double precision (FORTRAN real*8)
> classid = mxClassIDFromClassName('double')

mxClassIDFromClassName does not return an mwPointer, but an integer. See
http://www.mathworks.com/help/techdoc/apiref/mxclassidfromclassname.html.

>
> c Get variables passed from MATLAB, properly typed
> dx = mxGetPr(prhs(1))
>
> c Create MATLAB return matrices
> plhs(1) = mxCreateDoubleMatrix(10,10,classid,0)

mxCreateDoubleMatrix takes mwSize arguments. Unless you are compiling with
8-byte integers with your compiler, this is likely the cause of your crashing.
You need either provide an explicit interface so the compiler can do the cast,
or store the dimensions in variables of the appropriate type.

Chris

>
> return
> end
>
> With the above program, when I call it inside Matlab with:
>
> dt=0.25;
> testmx(dx);
>
>
> It crashed Matlab. Where is the problem?
>
>
> ~
> Brian Arnold <Brian.Arnold@mathworks.com> wrote in message
> <4D9E05C7.9020702@mathworks.com>...
>> Hi Alan,
>>
>> The problem is with the architecture of the gfortran compiler you installed
>> under /sw/, its libraries are 32-bit (i386). MATLAB needs to link 64-bit
>> (x86_64). You'll need to find a working 64-bit gfortran compiler whose
>> libraries are 64-bit and ABI-compatible with Apple's gcc.
>>
>> If you are building gcc yourself under /sw/ yourself, try rebuilding with the
>> -m64 option in CFLAGS, CXXFLAGS, and LDFLAGS.
>>
>> Brian
>>
>> Alan wrote:
>> > I am running MATLAB 2010b on MacOS 10.6.7. I wrote a simple mex file. > When
>> compiling it using mex command, I got the following error:
>> > > $ /Applications/MATLAB_R2010b.app/bin/mex advxmx.F ld: warning: in >
>> /sw/lib/gcc4.5/lib/libgfortran.dylib, file was built for i386 which is > not
>> the architecture being linked (x86_64)
>> > ld: warning: in >
>> /sw/lib/gcc4.5/lib/gcc/i386-apple-darwin10.6.0/4.5.0/libgfortranbegin.a, >
>> file was built for unsupported file format which is not the architecture >
>> being linked (x86_64)
>> > ld: warning: in >
>> /sw/lib/gcc4.5/lib/gcc/i386-apple-darwin10.6.0/4.5.0/libgcc.a, file was >
>> built for unsupported file format which is not the architecture being > linked
>> (x86_64)
>> > Undefined symbols:
>> > "_advx_", referenced from:
>> > _mexfunction_ in advxmx.o
>> > ld: symbol(s) not found
>> > collect2: ld returned 1 exit status
>> > > mex: link of ' "advxmx.mexmaci64"' failed.
>> > > My 'computer' says MACI64. And I listed this:
>> > $ ls /sw/lib/gcc4.5/lib/gcc/i386-apple-darwin10.6.0/4.5.0/
>> > crt3.o crtprec64.o include/ > install-tools/ libgcov.a plugin/ >
>> crtfastmath.o crtprec80.o include-fixed/ > libgcc.a libgfortranbegin.a x86_64/
>> > crtprec32.o finclude/ include-gnu-runtime/ > libgcc_eh.a libgfortranbegin.la
>> > Somehow the mex wants to link to library files in this directory, not > the
>> 'x86_64' directory inside it.
>> > > This happens to me with both MATLAB 2010a and 2010b, and on both an iMac >
>> and a MB Air. The gcc was installed using fink. My XCode version is > 3.2.5
>> 64-bit on one and 3.2.6 64-bit on another.
>> > > I also noticed that in the mexopts.sh file under my .matlab directory, >
>> in the maci64 section I have this:
>> > SDKROOT='/Developer/SDKs/MacOSX10.5.sdk'
>> > I am wondering why this root is not >
>> SDKROOT='/Developer/SDKs/MacOSX10.6.sdk' even though I already have both >
>> MacOSX10.5.sdk and MacOSX10.6.sdk under /Developer/SDKs/. This > mexopts.sh
>> was generated by mex -setup and is not changed.

Subject: Compiling 64-bit mex file on MacOS X

From: Steven_Lord

Date: 8 Apr, 2011 16:59:28

Message: 5 of 7



"Alan " <liuzr88@gmail.com> wrote in message
news:innd2l$os2$1@fred.mathworks.com...
> Brian,
>
> This for the answer. I downloaded a 64-bit version of gfortran and now
> the compliation went smoothly. However when I run the program it crashes
> Matlab. To isolate the problem, I stripped down my program to the minimum
> that still causing the crash, and here it is:
>
> #include "fintrf.h"
> subroutine mexfunction(nlhs,plhs,nrhs,prhs)
>
> c Required declarations for MATLAB
> mwPointer plhs(*),prhs(*)
> integer nlhs,nrhs
> c Prototype the return types of these functions
> mwPointer mxGetPr
> mwSize mxGetM, mxGetN
> real*8 mxGetScalar
> mwPointer mxCreateDoubleMatrix
>
> integer mxIsDouble, mxIsComplex
>
> c Pointer to classid used to create MATLAB single matrices
> mwPointer classid
>
> c Associate classid with double precision (FORTRAN real*8)
> classid = mxClassIDFromClassName('double')
>
> c Get variables passed from MATLAB, properly typed
> dx = mxGetPr(prhs(1))
>
> c Create MATLAB return matrices
> plhs(1) = mxCreateDoubleMatrix(10,10,classid,0)
>
> return
> end
>
> With the above program, when I call it inside Matlab with:
>
> dt=0.25;
> testmx(dx);
>
>
> It crashed Matlab. Where is the problem?

Does it still crash MATLAB if you call it like:

dt = 0.25;
y = testmx(dx);

If so, test nlhs before assigning to plhs(1). You should also test nrhs
before attempting to refer to prhs(1).

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Compiling 64-bit mex file on MacOS X

From: Christopher Hulbert

Date: 8 Apr, 2011 17:06:47

Message: 6 of 7

On 4/8/11 12:59 PM, Steven_Lord wrote:
>
> If so, test nlhs before assigning to plhs(1). You should also test nrhs before
> attempting to refer to prhs(1).
>

First, I agree that you should ALWAYS check nlhs and nrhs. You should also check
class types, sizes, etc. However in this case, I would like to point out a note
in the documentation for those that are not aware of it:

"It is possible to return an output value even if nlhs = 0. This corresponds to
returning the result in the ans variable."

http://www.mathworks.com/help/techdoc/matlab_external/f43721.html#bq8y4sa

Chris

Subject: Compiling 64-bit mex file on MacOS X

From: Alan

Date: 8 Apr, 2011 18:40:09

Message: 7 of 7

OK, now I tried this, just to check the number of input arguments:

#include "fintrf.h"
      subroutine mexfunction(nlhs,plhs,nrhs,prhs)

c Required declarations for MATLAB
        mwPointer plhs(*),prhs(*)
        integer nlhs,nrhs
c Prototype the return types of these functions
        mwPointer mxGetPr
        mwSize mxGetM, mxGetN, nx, nz
        real*8 mxGetScalar
        mwPointer mxCreateDoubleMatrix
        integer mxIsDouble, mxIsComplex

c Pointer to classid used to create MATLAB single matrices
        integer*4 classid

C Check for proper number of input and output arguments
        if (nrhs .ne. 1) then
           call mexErrMsgTxt('One input arguments required.')
        end if

        return
        end

When I run this in Matlab:

dx=0.25;
dy=0.5;
p = testmx(dx);

It complained
??? One or more output arguments not assigned during call to "testmx".
Error in ==> test_advxmx at 3
p = testmx(dx);

If I do this:
dx=0.25;
dy=0.5;
testmx(dx);
Nothing happens so no crash.

But if I do this
dx=0.25;
dy=0.5;
testmx(dx,dy);

That crashed Matlab again. It seems just by calling mexErrMsgTxt would crash.

BTW, I complied this mx program without any options:
/Applications/MATLAB_R2010b.app/bin/mex testmx.F

And the only thing I changed in my mexopts.sh file is in the maci64 section, on the path to gfortran. I changed it to:
/usr/local/gfortran/bin/gfortran'



Christopher Hulbert <cchgroupmail@gmail.com> wrote in message <inneb5$djn$1@dont-email.me>...
> On 4/8/11 12:28 PM, Alan wrote:
> > Brian,
> >
> > This for the answer. I downloaded a 64-bit version of gfortran and now the
> > compliation went smoothly. However when I run the program it crashes Matlab. To
> > isolate the problem, I stripped down my program to the minimum that still
> > causing the crash, and here it is:
> >
> > #include "fintrf.h"
> > subroutine mexfunction(nlhs,plhs,nrhs,prhs)
> >
> > c Required declarations for MATLAB
> > mwPointer plhs(*),prhs(*)
> > integer nlhs,nrhs
> > c Prototype the return types of these functions
> > mwPointer mxGetPr
> > mwSize mxGetM, mxGetN
> > real*8 mxGetScalar
> > mwPointer mxCreateDoubleMatrix
> >
> > integer mxIsDouble, mxIsComplex
> >
> > c Pointer to classid used to create MATLAB single matrices
> > mwPointer classid
> >
> > c Associate classid with double precision (FORTRAN real*8)
> > classid = mxClassIDFromClassName('double')
>
> mxClassIDFromClassName does not return an mwPointer, but an integer. See
> http://www.mathworks.com/help/techdoc/apiref/mxclassidfromclassname.html.
>
> >
> > c Get variables passed from MATLAB, properly typed
> > dx = mxGetPr(prhs(1))
> >
> > c Create MATLAB return matrices
> > plhs(1) = mxCreateDoubleMatrix(10,10,classid,0)
>
> mxCreateDoubleMatrix takes mwSize arguments. Unless you are compiling with
> 8-byte integers with your compiler, this is likely the cause of your crashing.
> You need either provide an explicit interface so the compiler can do the cast,
> or store the dimensions in variables of the appropriate type.
>
> Chris
>
> >
> > return
> > end
> >
> > With the above program, when I call it inside Matlab with:
> >
> > dt=0.25;
> > testmx(dx);
> >
> >
> > It crashed Matlab. Where is the problem?
> >
> >
> > ~
> > Brian Arnold <Brian.Arnold@mathworks.com> wrote in message
> > <4D9E05C7.9020702@mathworks.com>...
> >> Hi Alan,
> >>
> >> The problem is with the architecture of the gfortran compiler you installed
> >> under /sw/, its libraries are 32-bit (i386). MATLAB needs to link 64-bit
> >> (x86_64). You'll need to find a working 64-bit gfortran compiler whose
> >> libraries are 64-bit and ABI-compatible with Apple's gcc.
> >>
> >> If you are building gcc yourself under /sw/ yourself, try rebuilding with the
> >> -m64 option in CFLAGS, CXXFLAGS, and LDFLAGS.
> >>
> >> Brian
> >>
> >> Alan wrote:
> >> > I am running MATLAB 2010b on MacOS 10.6.7. I wrote a simple mex file. > When
> >> compiling it using mex command, I got the following error:
> >> > > $ /Applications/MATLAB_R2010b.app/bin/mex advxmx.F ld: warning: in >
> >> /sw/lib/gcc4.5/lib/libgfortran.dylib, file was built for i386 which is > not
> >> the architecture being linked (x86_64)
> >> > ld: warning: in >
> >> /sw/lib/gcc4.5/lib/gcc/i386-apple-darwin10.6.0/4.5.0/libgfortranbegin.a, >
> >> file was built for unsupported file format which is not the architecture >
> >> being linked (x86_64)
> >> > ld: warning: in >
> >> /sw/lib/gcc4.5/lib/gcc/i386-apple-darwin10.6.0/4.5.0/libgcc.a, file was >
> >> built for unsupported file format which is not the architecture being > linked
> >> (x86_64)
> >> > Undefined symbols:
> >> > "_advx_", referenced from:
> >> > _mexfunction_ in advxmx.o
> >> > ld: symbol(s) not found
> >> > collect2: ld returned 1 exit status
> >> > > mex: link of ' "advxmx.mexmaci64"' failed.
> >> > > My 'computer' says MACI64. And I listed this:
> >> > $ ls /sw/lib/gcc4.5/lib/gcc/i386-apple-darwin10.6.0/4.5.0/
> >> > crt3.o crtprec64.o include/ > install-tools/ libgcov.a plugin/ >
> >> crtfastmath.o crtprec80.o include-fixed/ > libgcc.a libgfortranbegin.a x86_64/
> >> > crtprec32.o finclude/ include-gnu-runtime/ > libgcc_eh.a libgfortranbegin.la
> >> > Somehow the mex wants to link to library files in this directory, not > the
> >> 'x86_64' directory inside it.
> >> > > This happens to me with both MATLAB 2010a and 2010b, and on both an iMac >
> >> and a MB Air. The gcc was installed using fink. My XCode version is > 3.2.5
> >> 64-bit on one and 3.2.6 64-bit on another.
> >> > > I also noticed that in the mexopts.sh file under my .matlab directory, >
> >> in the maci64 section I have this:
> >> > SDKROOT='/Developer/SDKs/MacOSX10.5.sdk'
> >> > I am wondering why this root is not >
> >> SDKROOT='/Developer/SDKs/MacOSX10.6.sdk' even though I already have both >
> >> MacOSX10.5.sdk and MacOSX10.6.sdk under /Developer/SDKs/. This > mexopts.sh
> >> was generated by mex -setup and is not changed.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
64bit mex mexop... Alan 7 Apr, 2011 11:29:06
rssFeed for this Thread

Contact us at files@mathworks.com