How do I find out what shared libraries my .exe file or .dll file depends upon?
MATLAB: How to find out what shared libraries the .exe file or .dll file depends upon
MATLAB
Related Solutions
The following steps create a C shared library on UNIX:
1. Compile your MATLAB files into a shared library (on UNIX)
mcc -t -L C -W lib:libFile -T link:lib -h <matlab-file> libmmfile.mlib
The -t option tells the MATLAB Compiler to translate the MATLAB code to the target language.
The -L option specifies the target language, which is chosen to be C.
The -W option tells the MATLAB Compiler to build a wrapper for libraries with the name specified after "lib:".
The -T option tellls the compiler what stage should be reached and for what intentions. Here we want to link our application together to build a shared library.
Specifying the libmmfile.mlib file tells the MATLAB Compiler to link against the MATLAB file Math routines that have already been precompiled into shared libraries when necessary.
This step will produce libFile.so, libFile.exports, libFile.h and libFile.mlib. You can add the
-g switch to produce a shared library suitable for debugging.
2. Make sure to call the initialization and termination routines from your code before calling any of the MATLAB Files that are compiled. The initialization routine for the libFile.so file would be:
libFileInitialize();
And the termination routine would be:
libFileTerminate();
All the routines (symbols) defined in 'libFile.so' will be in the libFile.export file and the prototypes for these routines will be libFile.h.
3. You can call the functions compiled from the MATLAB code by invoking mlf<MATLAB-file>(...), from your own C code. Where MATLAB-file represents the name of the original MATLAB-file with the first letter in upper case.
4. Compile your program and link against the MATLAB C Math Libraries as well as the libFile library created above.
Here is an example that illustrates the above steps.
Inside the MATLAB environment:
1. Write a MATLAB file function named foo and saved as foo.m that contains the following code:
function y = foo(x)y = x+1;
2. Use the MATLAB Compiler to compile foo.m into C code that can be included in a C shared library:
mcc -t -L C -W lib:libFoo -T link:lib foo.m libmmfile.mlib
This will generate the following files:
foo.c
foo.h
libFoo.c
libFoo.exports
libFoo.h
libFoo.mlib
libFoo.so
On certain UNIX platforms, the generated shared library file extension would be ".sl".
External to MATLAB:
1. Create the file called foowrap.c that calls the MATLAB Compiler generated shared library:
/* * foowrap.c * Calls a MATLAB Compiler generated shared library. * */ #include "libFoo.h" #include "matlab.h" #include <stdio.h> void main(void) { mxArray *x_ptr; mxArray *y_ptr; double *y; double ret; /* Create an mxArray to input into mlfFoo */ x_ptr = mxCreateDoubleScalar(1); /* Call the library initialization function */ libFooInitialize(); /* Call the implementation function */ y_ptr = mlfFoo(x_ptr); /* Call the library termination function */ libFooTerminate(); /* The return value from mlfFoo is an mxArray so we must extract the data from it */ y = mxGetPr(y_ptr); ret = *y; /* Print a double precision number*/ printf("The output of foo is %f\n",ret); }
2. Compile your application using one of the following methods:
With MBUILD (inside MATLAB):
mbuild foowrap.c -L./ -I./ -lFoo
In the above command, it is assumed that you are running the MBUILD command from the same directory in which you created the shared library. If this is not the case, then the -L and -I options should be followed by the locations of the shared library and the include files respectively.
Without MBUILD (outside MATLAB):
Building outside of MATLAB requires that you specify the locations of the MATLAB header files, the locations of the MATLAB shared libraries and the names of the MATLAB Libraries explicitly. With MBUILD this was not necessary because MBUILD finds and uses these files automatically. Actually, a good way to find these libraries would be to run MBUILD with the "-v" flag and use the compile and linker commands from the verbose output. If you do not have the possibility to execute the MBUILD command in MATLAB in verbose mode, please note that the required libraries to build your application outside MATLAB can be found in the following folders, in which "<mcr_root>" is the folder in which MATLAB Compiler Runtime (MCR) is installed on your computer:
<mcr_root>/<mcr_version>/extern/lib/glnx86 (or glnxa64) <mcr_root>/<mcr_version>/bin/glnx86 (or glnxa64) <mcr_root>/<mcr_version>/extern/include
The "<mcr_version>" listed above is the version of the installed MCR. For instance, the MCR version related to MATLAB R2014b is 8.4 and therefore:
<mcr_version> --> v84
Please refer to the following link for a complete list of MCR versions:
Below is the command, if you were building using GCC outside MATLAB:
gcc -o foowrap foowrap.c -L/<mcr_root>/<mcr_version>/extern/lib/glnx86 -L/<mcr_root>/<mcr_version>/bin/glnx86 -L./ -I/<mcr_root>/<mcr_version>/extern/include -I./ -lmmfile -lmatlb -lmx -lut -lm -lFoo
3. Set the search paths for the dynamic loader to find the shared library. The exact command depends on the exact UNIX operating system and the shell you are running. The following command assumes you are running in a csh shell.
setenv LD_LIBRARY_PATH <matlabroot>/bin/<arch>:<matlabroot>/extern/bin/ <arch>:<matlabroot>/sys/os/<arch>/
If the LD_LIBRARY_PATH was previously defined, the command would be:
setenv LD_LIBRARY_PATH <matlabroot>/bin/<arch>:<matlabroot>/extern/bin/ <arch>:<matlabroot>/sys/os/<arch>/:$LD_LIBRARY_PATH
4. Run the executable file, "foowrap" that was created:
[unix]%> foowrap
The output of foo is 2.000000
NOTE: As of R2013a 'fopen', 'fclose' and 'fprintf' are supported for code generation, please navigate to the following link for more information:
For releases prior to R2013a, please refer to the details below:
MATLAB Coder currently does not generate code for file IO functions such as fopen, fclose, and fprintf in release R2011a. Please use the function coder.ceval to call user supplied C/C++ functions in place of these MATLAB file IO functions. For example, one can use coder.ceval to call standard C utilities such as fopen, fclose, and fprintf. In this case, one would need to add a "#include" statement for it to the source code in the header file. This can be achieved by
configuration_object.CustomHeaderCode = '#include <stdio.h>';
Attached please find an example of generating C/C++ code with file IO commands using the MATLAB Coder. The example demonstrates how to write a string and a matrix to a text file using the standard C library utilities, such as "fopen", "fprintf", and "fclose" from the stdio library.
The file to execute in MATLAB is "generate_matlab_coder_file_io_example.m". This file sets up the necessary configurations for code generation and an executable file called file_printing_example.exe will be generated. The MATLAB source file to be built (file_printing_example.m), and all of its MATLAB dependencies are located within the "source_code" folder. When you run the executable file, a text file ('test.txt') will be generated which will contain the text string and the matrix.
MATLAB comments are provided throughout the code to explain the details and the workflow. Please feel free to modify the code to tailor it to your application.
Related Question
- Is there an example on how to use C++ shared libraries with Microsoft Visual C++ 2005 Professional or Express editions
- How to determine which libraries the MEX-file or stand-alone application requires
- How to call a DLL created from Simulink model in a Visual Studio application
- How to use Dependency Walker with MATLAB
- How to create a C – shared library with MATLAB Compiler 3.0 which can be used in other projects
- How to create a C shared library that contain Matlab GUI function, and how to call the function from visual studio 2008
- In order to use the compiled mex function windows SDK 7.1 is required… Does anyone know why
Best Answer