How can I use CLAPACK,BLAS or LAPACK in a mex file? - mex

I am having trouble writing a MEX file in MATLAB that can perform a simple linear operation such as taking the inverse of a matrix. I have successfully managed to take the inverse of a matrix using Visual Studio 2010 and have successfully created a MEX file hence the only thing I am having trouble is getting these two concepts together. I have tried to compile a MEX example code that I got from the MathWorks site but with no luck.
Here is what I have tried,
Saved the file (renamed it) I got from MathWorks as .c extension and then tried to compile it in MATLAB got:
Creating library C:\Users\CIT\AppData\Local\Temp\mex_bKHjrl\templib.x
and object C:\Users\CIT\AppData\Local\Temp\mex_bKHjrl\templib.exp
eko1.obj : error LNK2019: unresolved external symbol dgesv referenced
in function mexFunction eko1.mexw64 : fatal error LNK1120: 1
unresolved externals
Also I tried to compile it as a .cpp file however an error occurred because it didnt recognize the memcpy function.
Since these didn't work I wrote my own program that used the subroutines dgetrf and dgetri from the LAPACK library however an error occured:
c:\users\cit\documents\matlab\f2c.h(16) : error C2371: 'complex' :
redefinition; different basic types
C:\Program Files\MATLAB\R2011b\extern\include\lapack.h(39) : see declaration of 'complex' c:\users\cit\documents\matlab\f2c.h(17)
: error C2371: 'doublecomplex' : redefinition; different basic types
C:\Program Files\MATLAB\R2011b\extern\include\lapack.h(40) : see declaration of 'doublecomplex' eko2.cpp(29) : error C2057:
expected constant expression eko2.cpp(29) : error C2466: cannot
allocate an array of constant size 0 eko2.cpp(29) : error C2133:
'ipiv' : unknown size eko2.cpp(33) : error C2664: 'dgetrf' : cannot
convert parameter 1 from 'integer *' to 'ptrdiff_t *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast eko2.cpp(34) :
error C2664: 'dgetri' : cannot convert parameter 1 from 'integer *' to
'ptrdiff_t *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Any help you guys give me would be extremely appreciated
Thanks in advance.

Without any code to see what you've tried, it's hard to tell, but... in order to properly compile a mex file in matlab that depends on other libraries, you need to specify those libraries in the compile command. Use mex filename.c -v -l*libraryname*.lib. The -l switch indicates to the compiler that you are specifying a library that you want to include. If this library is not found, I would include the full path to the library in the command. I hope that provides you with some assistance. Using this methodology has been successful for me.

Related

how to fix Error c2373 InterlockedCompareExchange redefinition: different type modifier

I have just added the QT dll to a new C++ project in Visual Studio 2010, no code added. But when I build the project I have error message:
Error 62 error C2373: '_InterlockedCompareExchange' : redefinition; different type modifiers C:\Users\bap\Desktop\cPlus\LIBRARY\QtCore\qatomic_msvc.h 128 // Code Clarity
Please tell me how to fix that.
I had the same problem. I solved it by trying different different calling conversions in your project.(e.g. from stdcall to cdecl)

loadlibrary in 64-bit Matlab?

I am trying to load a C++ API (for the BB60C radio at SignalHound.com) into Matlab using loadlibrary() but it is only working in 32-bit Matlab (R2014a). The zip file here contains the bb_api.h header file and has both a 32-bit and 64-bit bb_api.dll in their respective folder. To run it, I copy the correct .dll into the folder with bb_api.h and call loadlibrary('bb_api','bb_api.h'). While this works for the 32-bit version, when I use the 64-bit .dll in 64-bit Matlab, I get the following error:
Error using loadlibrary (line 422)
Building bb_api_thunk_pcwin64 failed. Compiler output is:
cl -I"C:\Program Files\MATLAB\R2014a\extern\include" /W3 /nologo /D_CRT_SECURE_NO_DEPRECATE
/D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0 -I"C:\Program Files\MATLAB\R2014a\extern\include" -I"C:\Program
Files\MATLAB\R2014a\simulink\include" -I"C:\Users\mimhof\Documents\GitHub\SDR\Matlab"
-I"C:\Users\mimhof\Documents\GitHub\SDR\Matlab" "bb_api_thunk_pcwin64.c" -LD -Fe"bb_api_thunk_pcwin64.dll"
bb_api_thunk_pcwin64.c
C:\Users\mimhof\Documents\GitHub\SDR\Matlab\bb_api.h(202) : error C2061: syntax error : identifier 'bbOpenDevice'
C:\Users\mimhof\Documents\GitHub\SDR\Matlab\bb_api.h(202) : error C2059: syntax error : ';'
C:\Users\mimhof\Documents\GitHub\SDR\Matlab\bb_api.h(202) : error C2059: syntax error : 'type'
C:\Users\mimhof\Documents\GitHub\SDR\Matlab\bb_api.h(203) : error C2061: syntax error : identifier 'bbCloseDevice'
C:\Users\mimhof\Documents\GitHub\SDR\Matlab\bb_api.h(203) : error C2059: syntax error : ';'
C:\Users\mimhof\Documents\GitHub\SDR\Matlab\bb_api.h(203) : error C2059: syntax error : 'type'`
and so on for all the methods available in the API. What would cause this error to occur in 64-bit and not 32-bit?
Thanks!
You talk about using C++, but your file is named .c, which triggers compilation using the C language rules. The Microsoft compiler's C support is pretty outdated, and it's likely that is the source of your problems. You may want to use C++ instead.
Beyond that, take a close look at the line where the error occurs, just as if you were fixing compile errors in your own code.
The ***_thunk.c file is auto-generated by a perl script shipped with matlab, and is the source of many similar errors.
This fact (the auto generated thunk source is in c) is the source of many documented seemingly weird loadlibrary limitations and many more undocumented ones.
My solution when being hit by a very similar issue was to hijack this auto-generated file, rename it to cpp and build my own cpp version of the thunk. This required some hacking, as this file lifespan is a fraction of a second. Full details are here.

Difference Between WINAPI in C and C++

I am reading data from driver. The driver came with examples on how to develop applications based on the driver. The examples were written few years back. They use WINAPI and C. Now I will use their some header files. They have data structures and various other stuffs defined.
I tried creating a WINAPI in C++ and tried to link to those files. But as explained here, in the last answer, I very much believe that the same is problem with my code.
Now, I can't do as suggested there. My programs are long and I can't mess header files. They are complicated.
My option is to create my project entirely in C (I hope, it solves).
First, I renamed the file .cpp t0 .c.
(I don't even know the difference between these two programming languages. Their difference made no difference so far.)
MAIN QUESTION
I used file IO using API and used following:
HANDLE myFile=CreateFile("filename.txt",FILE_APPEND_DATA,FILE_SHARE_WRITE,0,\
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
int BufferNo=sprintf(body,"%.5f %[3].5f %[3].5f %[3].5f %[3].5f %[3].5f %[3].5f \
%[3].5f %[3].5f \n",a1,a2,a3,a4,a5,a6,a7,a8,a9);
WriteFile(myFile,body,lstrlen(body),0,NULL);
CloseHandle(myFile);
Problem is it does not compile. Says errors like:
error C2275: 'HANDLE' : illegal use of this type as an expression
error C2146: syntax error : missing ';' before identifier 'myFile'
error C2065: 'myFile' : undeclared identifier
warning C4047: '=' : 'int' differs in levels of indirection from 'HANDLE'
error C2143: syntax error : missing ';' before 'type'
error C2065: 'myFile' : undeclared identifier
warning C4022: 'WriteFile' : pointer mismatch for actual parameter 1
error C2065: 'myFile' : undeclared identifier
warning C4022: 'CloseHandle' : pointer mismatch for actual parameter 1
These were not errors in C++ and they compiled, just did not link. How do I solve this.
Note, there is similar function I have used before this. Which is not detected as error.
HANDLE myFile=CreateFile("filename.txt",GENERIC_WRITE,FILE_SHARE_WRITE,0,\
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,0);
char* HeadingStr="a1(m) a2(m) a3(m) a4(m) a5(m) a6(m) a7(m)\
a8(m) a9(m)\n";
WriteFile(myFile,HeadingStr,lstrlen(HeadingStr),0,NULL);
CloseHandle(myFile);
This does not show any error. It is in same file and is before, in different function above errors. This is in WinMain and above is in WndProc function.
The Visual Studio compiler does not support C99, which is when inline declarations were added. You need to declare all of your variables at the beginning of your functions, or switch to a compiler that supports C99.

Unable to compile MEX

I am trying to compile some MATLAB MEX files from C++ source. The files I am trying to compile can be found here; I am using Windows XP on a 32-bit system, MATLAB 2012a and have installed the Microsoft Windows SDK v7.1 to use as my compiler.
MATLAB outputs the following error if I try to compile:
>> mex -O -largeArrayDims osc_free_address.c
osc_free_address.c
osc_free_address.c(1) : error C2059: syntax error : '<'
osc_free_address.c(37) : error C2015: too many characters in constant
osc_free_address.c(38) : error C2059: syntax error : '<'
osc_free_address.c(39) : error C2015: too many characters in constant
osc_free_address.c(39) : error C2015: too many characters in constant
osc_free_address.c(39) : error C2015: too many characters in constant
osc_free_address.c(41) : error C2018: unknown character '0x40'
osc_free_address.c(43) : error C2015: too many characters in constant
osc_free_address.c(44) : error C2015: too many characters in constant
osc_free_address.c(44) : error C2015: too many characters in constant
osc_free_address.c(44) : error C2015: too many characters in constant
osc_free_address.c(44) : error C2015: too many characters in constant
osc_free_address.c(46) : error C2059: syntax error : '<'
osc_free_address.c(82) : fatal error C1021: invalid preprocessor command 'page'
C:\PROGRA~1\MATLAB\R2012A\BIN\MEX.PL: Error: Compile of 'osc_free_address.c' failed.
Error using mex (line 206)
Unable to complete successfully.
I have used mex -setup to setup my compiler with Microsoft Windows SDK v7.1. Interestingly, although I have them installed on my system, MATLAB doesn't see MS Visual Studio C++ 2008 or 2010. I have also tried compiling with the Lcc-win32 C 2.4.1 compiler (which is recognised by MATLAB), to the same effect as shown above.
I understand that this particular set of C++ source files might require something called liblo to compile (I'm not sure whether this is a requirement at compile-time or run-time); as far as I'm aware I have installed these correctly, but there's a possibility that the problem might lie here too.
Any help would be greatly appreciated.
Try renaming your .c files to .cpp and re-compile
>> mex -largeArrayDims -O osc_free_address.cpp
Moreover, why don't you try the build m file that comes with the package
(as suggested in this question)?
Have you downloaded and compiled liblo? you might need to add the include path and library path for it in the mex command?
>> mex -largeArrayDims -O -I<liblo include path> -L<liblo lib path> -llo osc_free_address.cpp

Compiling and using NTL c++ library for Windows

I have compiled the NTL inifite precision integer arithmetic library for c++, using Microsoft Visual Studio 2008. I did as explained, on this site, using the Visual Studio interface, rather than from the command prompt. Actually I would rather do it from the command prompt, but I was not sure how to.
Anyhow, I got the library compiled, and I now want to compile a program using the library, from the command prompt. The program I am trying to compile, has been tested on a linux system, where I compile it with the following
c++ -I/appl/htopopt/Linux_x86_64/NTL-5.4.2/include mpqs.cpp main.cpp -o main -L/appl/htopopt/Linux_x86_64/NTL-5.4.2/lib -lntl -L/appl/htopopt/Linux_x86_64/gmp-4.2.1/lib -lgmp -lm
Nevermind the gmp stuff, I dont have that installed on Windows. It is purely an optional thing that will make the NTL run faster. Anyhow, this works fine on linux. Now on Windows I write the following
cl /EHsc /I D:\Downloads\WinNTL-5_5_2\include mpqs.cpp main.cpp /link /LIBPATH:"D:\Documents\Visual Studio 2008\Projects\ntl\Debug"
But this results in the following errors:
mpqs.cpp
mpqs.cpp(38) : error C2039: 'find_smooth_vals' : is not a member of 'QS'
d:\desktop\qs\mpqs.h(12) : see declaration of 'QS'
mpqs.cpp(41) : error C2065: 'M' : undeclared identifier
mpqs.cpp(41) : error C2065: 'n' : undeclared identifier
mpqs.cpp(42) : error C2065: 'sieve_table' : undeclared identifier
mpqs.cpp(42) : error C2228: left of '.size' must have class/struct/union
type is ''unknown-type''
mpqs.cpp(43) : error C2065: 'sieve_table' : undeclared identifier
mpqs.cpp(44) : error C2065: 'qx_table' : undeclared identifier
mpqs.cpp(44) : error C3861: 'test_smoothness': identifier not found
mpqs.cpp(45) : error C2065: 'smooth_indices' : undeclared identifier
mpqs.cpp(45) : error C2228: left of '.push_back' must have class/struct/union
type is ''unknown-type''
main.cpp
Generating Code...
It is as if, my mpqs.h file is not included into the compilation process? Also I dont understand why it complains about .push_back() for a vector type?
Help is much appreciated!
mpqs.h is definitely being included as the output asks you to refer to it.
Seeing as MPQS.h does not appear to be included in the NTL library ... did you write it? If so can you post the code up?
Also, shouldn't you included the library file somewhere on your build?
Edit: There is no function find_smooth_values so why should you expect MSVC to find it? I'm not sure why that compiles under GCC but its obviously missing. I'm guessing that the other errors are caused because of this one. The errors are telling you things. You should listen to them.
push_back is failing because it doesn't know what the type you are trying to push_back into is. This again is probably caused by the fact that find_smooth_values doesn't exist. Try adding the function prototype into the QS class. This may well fix all your problems.
As for the library it won't get to using the library until the compilation succeeds. So don't worry about that just now. Get in there and fix the errors that MSVC is reporting!