1
votes

I'm compiling a C file (part of voc-release library) with Matlab and I'm getting the error below. I can't seem to solve it. Could anyone tell me what causes this error and what I can do about it?


    mex -O features.cc
    Writing library for features.mexw32 
    c:\users\safaa\appdata\local\temp\mex_ty~1\features.obj .text: undefined reference to '_round' 
    C:\PROGRA~1\MATLAB\R2009A\BIN\MEX.PL: Error: Link of 'features.mexw32' failed.


1
What mex compiler are you using? See mex -setup.Nzbuu
I've tried the two compilers via this instruction "mex -setup" the one included in matlab and the visual c++ the first one runs on some files but the second one doesn't run on the those files , .h files fatal errorsSafeY

1 Answers

2
votes

If you had included more of the error message, I believe that this would have already been answered, just an FYI. It looks like you're compiling a file from some non-matlab source, which is fine, but you're likely going to have to give more information about it. It appears that the features.cc file is referencing a symbol round which is why you're getting the error. You'll need to provide the library for the function, or compile it from source. If you do a help mex, it should be able to give you information about the library linker commands -L for the path and -l for the library. I seem to remember that this feature doesn't work exactly as advertised, and requires you to use -lC:/path/to/library/libfile.lib, or whatever. First of all, I would recommend compiling the yprime example from the Matlab help. This will ensure that you have your compiler setup correctly, which it sounds likel you do. Also, take a look at the yprime.c file and the mexFunction. You may want to make sure the compiler will accept a .cc file as a C file. It may interpret it as a C++ file which will cause you more headaches. You could rename the yprime.c file to yprime.cc just to test the idea.

EDIT Thanks for providing the code. This should be cake my friend, complete cake. :-)

This is my error I get when I try to compile using:

Microsoft Visual C++ 2008 Express

>> mex -v -g features.c
This is mex, Copyright 1984-2007 The MathWorks, Inc. 

-> Default options filename found in C:\Documents and Settings\wynkocl\Application\Data\MathWorks\MATLAB\R2009b 
---------------------------------------------------------------- 
->    Options file           = C:\Documents and Settings\wynkocl\Application Data\MathWorks\MATLAB\R2009b\mexopts.bat 
      MATLAB                 = C:\MATLAB\R2009B~1 
->    COMPILER               = cl 
->    Compiler flags: 
         COMPFLAGS           = /c /Zp8 /GR /W3 /EHs /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0 /DMATLAB_MEX_FILE /nologo /MD 
         OPTIMFLAGS          = /O2 /Oy- /DNDEBUG 
         DEBUGFLAGS          = /Z7 
         arguments           =  
         Name switch         = /Fo 
->    Pre-linking commands   =  
->    LINKER                 = link 
->    Link directives: 
         LINKFLAGS           = /dll /export:mexFunction /LIBPATH:"C:\MATLAB\R2009B~1\extern\lib\win32\microsoft" libmx.lib libmex.lib libmat.lib /MACHINE:X86 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /incremental:NO /implib:"C:\DOCUME~1\WYNKOCL\LOCALS~1\TEMP\MEX_7L~1\templib.x" /MAP:"features.mexw32.map" 
         LINKDEBUGFLAGS      = /DEBUG /PDB:"features.mexw32.pdb" 
         LINKFLAGSPOST       =  
         Name directive      = /out:"features.mexw32" 
         File link directive =  
         Lib. link directive =  
         Rsp file indicator  = @ 
->    Resource Compiler      = rc /fo "mexversion.res" 
->    Resource Linker        =  
---------------------------------------------------------------- 


--> cl  /c /Zp8 /GR /W3 /EHs /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0 /DMATLAB_MEX_FILE /nologo /MD /FoC:\DOCUME~1\WYNKOCL\LOCALS~1\TEMP\MEX_7L~1\features.obj -IC:\MATLAB\R2009B~1\extern\include -IC:\MATLAB\R2009B~1\simulink\include /Z7 -DMX_COMPAT_32 features.c 

features.c 
features.c(27) : error C2059: syntax error : 'type' 
features.c(28) : error C2059: syntax error : 'type' 
features.c(92) : warning C4013: 'round' undefined; assuming extern returning int 

  C:\MATLAB\R2009B~1\BIN\MEX.PL: Error: Compile of 'features.c' failed.

Ahhh...now we can find the issue. First, the C2059 error is do to the fact the the function max is being redefined, I'm pretty sure on that because that's how I fixed it. Then the round function has no prototype. Hmmm, that's odd. Well, that's because round is not in math.h so you'll need to implement one like at the top of your file like so:

int round(double number)
{
  return (number >= 0) ? (int)(number + 0.5) : (int)(number - 0.5);
}

Now it compiles! As a tip, you can also try mex -v -g when you build to get more verbose debugging messages. Hope this gets you on your way!

Here's the last of what I've got here:

  1. Re-download voc-release4.01.tgz
  2. Extract and rename all .cc files .cpp files.
  3. Open the compile.m script and modify like so:

    mex -v -g resize.cpp mex -v -g dt.cpp mex -v -g features.cpp mex -v -g getdetections.cpp

    % use one of the following depending on your setup % 0 is fastest, 3 is slowest

    % 0) multithreaded convolution using SSE % mex -v -g fconvsse.cpp -o fconv

    % 1) multithreaded convolution using blas % WARNING: the blas version does not work with matlab >= 2010b % and Intel CPUs % mex -O fconvblasMT.cpp -lmwblas -o fconv

    % 2) mulththreaded convolution without blas % mex -O fconvMT.cpp -o fconv

    % 3) convolution using blas mex -g -v fconvblas.cpp -LC:\MATLAB\R2009bSP1\extern\lib\win32\microsoft -lmwblas -output fconv

    % 4) basic convolution, very compatible % mex -O fconv.cpp -o fconv

I recommend staying with option 3, pthreads is likely just a little too much for you at this point in time. :-)

  1. resize.cpp

Add #define bzero(b,len) (memset((b), '\0', (len)), (void) 0) int round(double number) { return (number >= 0) ? (int)(number + 0.5) : (int)(number - 0.5); } at the top and change:

alphainfo ofs[len];

to

alphainfo * ofs = (alphainfo *)mxMalloc(len);

and

assert(sy-1 >= 0);

to assert(sy1 >= 0); don't forget to mxFree(ofs); at the end of the function.

  1. dt.cpp: Change all int32_t to int32_T.
  2. features.cpp Add

    int round(double number) { return (number >= 0) ? (int)(number + 0.5) : (int)(number - 0.5); }

  3. Make sure you have a directory C:\MATLAB\R2009bSP1\extern\lib\win32\microsoft, if not put your Matlab release extern library directory there.

Enjoy.