LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Linking Matlab generated .lib files

Hi All,
 
I recently have tried using Matlab to created a shared c library from a .m file.  When I create this library, I get back a .dll, .h file, .c file, and a .lib file that I can then add to a new CVI project. I use these to access the various functions that I created in the .m file and that were subsequently compiled into a .dll.  My problem occurrs when I link the .lib file to my CVI project and then try to compile it.  When I try to compile the project with the .lib file simply added to my project I get the following message and I am unable to compile the project
 
"Read Error
Aborted load of library <matlab generated library name>"
 
When I attempt to use LoadExternalModuleEx instead of linking the .lib file statically, I get an error code of -4 returned, and I can't load the library file.
 
I'm assuming this message means that however matlab makes it's .lib file, it isn't compatable with what CVI is looking for.  I've never had this problem with CVI before, and I didn't even know that there are different formats of .lib files out there.  Is there any tool that CVI might have that allows a person be able to convert a non-standard .lib file into something CVI recognizes?  If not, has anyone else encounterd this problem and might have some suggestions about how to fix it?
 
Using:
Labwindows/CVI 7.0
Matlab 7.0.1 (rev14)
Windows XP
0 Kudos
Message 1 of 8
(11,919 Views)

I don't have matlab so i cannot test this out. CVI 7.0 can use import libraries created with VC++ and Borland C++ compilers. But you need to make sure that function names are exported as unmangled C style functions. You can use tools like dependency walker to see the exports of the DLL. If you see something like fuZZ?Aunc?@tion?Name (instead of functionname) as the exported symbol, this means that matlab is generating symbols for use with a C++ compiler. Typically in VC++, you would mark the exported function using extern "C" to specifiy to the compiler that the function needs to be exported in C. I'm not sure how this is done with matlab.

If this does not turn out to be the case, attach the import library , header and dll and I can look into this some more.

Thanks

Bilal Durrani
NI
0 Kudos
Message 2 of 8
(11,908 Views)

Hil Bilal,

The .DLL file seemed to be OK when I checked it out in dependency walker.  The only odd thing that I saw was when I opened dependency walker, only one of the windows had function names present in it (see attached .jpg).  However, I haven't been able to find if this will affect my application. 

I am also attaching a .zip file of two .dll, .lib, .h files.  The Libmatrix .lib is the one where the .lib file returns a read error in CVI and was generated by my matlab compiler.  The meanfunctionlib .lib file can be loaded by CVI.  This was file that I downloaded from the codeproject for test purposes and it was made with an earlier version of the matlab compiler that I don't have access to.  I thought you might be able to compare the two and see what is wrong.

Thanks for your help so far!

Download All
0 Kudos
Message 3 of 8
(11,905 Views)
Hi Bilal,
 
Thank you for your help but it seems I've been able to solve this problem.  I guess when building c shared libraries with matlab, it can use an external compiler to generated the files.  I was able to change this setting from whaterver it was (perhaps a proprietary matlab compiler) to my Visual Studio compiler and the .lib file seems to load just fine now in labwindows. 
 
Thanks again for your help.
0 Kudos
Message 4 of 8
(11,896 Views)
Dear DaveC,

If you find a clear way for porting Matlab programs via DLL to Labwindows/CVI, could you please share them with us?
A kind of step-by-step explanation would be very appreciated...

Thank you very much.


0 Kudos
Message 5 of 8
(11,808 Views)
Hi gserver,
First thing to mention is that I have found that in order for many of the functions I use to work I couldn't use the generic matlab C compiler i.e. lcc when compiling the .DLL.  Instead I had to use MS Visual C/C++ 7.0 compiler to compile the .dll.  You will have this compiler if you have MS Visual Studio .NET on your machine.  I would suggest using this compiler or if you don't have it get it. Here are the steps...
1st time steps
1.) at the command prompt type mbuild -setup
2.) at the mbuild prompt select MS Visual C/C++ 7.0 compiler
Every time you need to recompile
1.) at the command prompt type mcc –b csharedlib:[target DLL name] [target matlab files].m
Integrating the compiled DLL with your CVI project
1.) The following files need to be accessable by your CVI project from the [matlab]\ extern\include directory
-         Matlab.h
-         matrix.h
-         mclmcr.h
-         libmatrix.h
-         tmwtypes.h
2.) The following files need to be linked to your CVI project from the [matlab]\ extern\lib\win32\microsoft\msvc71 directory
-         mclmcr.lib
-         libmx.lib
 
3.) You will need to link “mclmcr.h” and “libmatrix.h” files to your project explicitely by using the #include
 
Other random things:
 
1.) When you create you .DLL you will also see a .ctf file created.  You must carry this file along with your .DLL or the .DLL will not initialize.  The .ctf file makes some various directories when you initialize the .DLL.
2.)  The first time you run the .DLL it may take a couple minutes to init but after that it's pretty quick
3.) If you do any type of matrix addition, mult, subt, division using the . operator and one matrix contains all whole numbers and the second matrix contains floating point numbers you will need to use the "double" function around bot matrix's in the MATLAB code, otherwise the .DLL will fail when you try to run it
4.) The matlab runtime engine must be installed on the target machine using the compiled .DLL this is a 90 meg file that can be found in your matlab directory <matlab>\toolbox\compiler\deploy\win32.
 
The following links are also pretty helpful
 
 
All of this assumes you are using Labwindows 7.0 and Matlab converter 4.2.  I can't speak for this stuff being true otherwise

 
Message 6 of 8
(11,770 Views)
Thank you Mr DaveC,

You had made a wonderfull explanation and had given nice references than my expectations,

So following similar methodologies to yours we can build very powerful programs with other tools. Such as Mathematica (which is my favourite), Maple, Mathcad, Labview (which is known by everyone)...

But some programs maybe more easy to execute with ActiveX callings especially small ones like Vissim (I dont know exactly that this program support ActiveX) but there are many powerfull application specific tools out there for scientific usage.

I have to work some on this methods than maybe we can prepare a good tutorial or web-site about porting countless scientific applications to CVI...
0 Kudos
Message 7 of 8
(11,761 Views)

Hi, Dave:

I have a very similar task as your post message, run in CVI platform, sending measured data from CVI to Matlab by using shared LIB, and get feedback results from Matlab. The development enviroment:  Labwindow/CVI 8.1, Matlab 7.0.1 /rev14), windows XP

I have done step by step as you posted instructions. As I run by CVI with command: mylibInitialize(); and mylibTerminate(); I got error message: Missing prototype.

 

Can you please send me your simple sample code?

Or i can send you me code (very simple, just for try code), need you kindly to take a check.

Looking forward to your helps.

Jim

0 Kudos
Message 8 of 8
(9,832 Views)