1

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.


Ali
  • 935
  • 2
  • 9
  • 10
SafeY
  • 287
  • 2
  • 6
  • 18
  • What mex compiler are you using? See `mex -setup`. – Nzbuu Mar 07 '12 at 14:11
  • 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 errors – SafeY Mar 07 '12 at 18:25

1 Answers1

2

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.

macduff
  • 4,655
  • 18
  • 29
  • I've tried other files c & cc and they compile correctly but with others like the features.cc m it's giving me that error and with the rest it says that there are missing header files !! when I bring them all and put them in the "include directory " the errors turned to be in the header files itself !!! It's driving me crazy , I can't find the solution ! some pages on the net says that there is a windows error :S I couldn't understand all your reply :$ – SafeY Mar 08 '12 at 13:13
  • @PureSoul would you consider posting your features.cc file? – macduff Mar 08 '12 at 14:17
  • sure , I don't know If it's allowed to post it here :$ – SafeY Mar 08 '12 at 17:11
  • Actually that's not my code , There's a tool called "voc-release4.01" [link] http://www.cs.brown.edu/~pff/latent/ and features.cc is a file from it , there were a horrible mistakes in the c files !!! I fixed them all , yet still this linking problem and the missing libraries I just want to make this tool runs , I really need it in my work :( – SafeY Mar 08 '12 at 17:16
  • It's compiling !!!!!!!!! :D so many Big Thank you Mr.macduff I'm gonna try to compile the other files now :) – SafeY Mar 08 '12 at 18:01
  • [link] http://dl.dropbox.com/u/62218293/fconvsse.cc This one was giving me errors related to a missing library and after I searched the net and put in in the include folder the errors are now from the .h file itself !!! ------------------------ [link] http://dl.dropbox.com/u/62218293/getdetections.cc and this one is giving me errors related to parameters passing by reference in the prototype of the function , do you know what's the problem ? – SafeY Mar 08 '12 at 18:09
  • I've fixed the errors in the " getdetections.cc " please help me with the other one ! I can't find the Bug :( – SafeY Mar 08 '12 at 20:00
  • I really appreciate your help , I did as you said but matlab becomes stupid ! C:\PROGRA~1\MATLAB\R2009A\BIN\MEX.PL: Error: 'resize.cpp' not found. I've changed the path , still it's not working – SafeY Mar 08 '12 at 20:24
  • I'm sorry , but the code itself of the cc files contains errors !! changing it from cc to ccp didn't help :( I'll stay on my files that I had fixed one by one ! one problem remains that there's a missing library I downloaded it then it asked me to get another one called and I did it , now all the errors come from this sched.h !!! can you try it on your laptop ? here are the missing headers : [link]http://dl.dropbox.com/u/62218293/missing%20.h.rar thank you so much , try the "fconvsse.cc"file – SafeY Mar 08 '12 at 20:51