I would like to be able to create C library ('.lib' file) from my model. Using 'ert_shrlib.tlc' target, it is possible to create shared library (.dll files). I would like to know if it is possible to generate a '.lib' file too.
MATLAB: How to create C library (‘.lib’ file) from the model in Real-Time Workshop 6.6 (R2007a)
ertlibrarieslibrarysimulink coderstatic
Related Solutions
The TMF file that the documentation refers is the TMF file for the compiler that you have set. Suppose you are using Visual C compiler, you can find the actual tmf file under the following directory:
~matlabroot/rtw/c/ert/ert_vc.tmf
Where 'matlabroot' is the installation directory of your version of MATLAB.
In this file, search for the following section:
#--- Comment out the next line to retain .lib and .exp files --- @del $(RELATIVE_PATH_TO_ANCHOR)\$(MODEL)_win32.lib $(RELATIVE_PATH_TO_ANCHOR)\$(MODEL)_win32.exp
As indicated above, if you comment out this section it should retain the '.lib' file.
For the point about modifying the header files:
There are two ways using DLLs:
1. Explicit : This way of using DLLs is demonstrated in the demo named 'rtwdemo_shrlib.mdl' and related examples. This way of linking is more portable (DLL are compiler independent, LIB are compiler dependent)
2. Implicit : Keep the import library LIB file and link against this LIB file. For MS VC, it is required to have every Symbol that would be in such imported library to be decorated with __declspec(dllimport) so that the user's application(caller of the functions in dll) , which should #include a header file that describes the DLL modules/data, can find the correct symbol in the DLL. The aforementioned header file can be derived from our generated model.h but must be modified as document stated.
A simple example:
In the model.h
extern void nestedop_step(void);
shall be changed as in the modified header
__declspec(dllimport) void nestedop_step(void);
This usage is more on the caller of the DLL side thus we would recommend the user to check examples on Microsoft website about how to call a function in DLL.
<http://msdn.microsoft.com/en-us/library/3y1sfaz2(VS.80).aspx>
This change has been incorporated into the documentation in Release 14 Service Pack 3 (R14SP3). For previous releases, read below for any additional information:
Here is the recommended procedure for using Real-Time Workshop 5.0 (R13) or 5.1 (R13SP1) in a cross-platform development environment. These steps describe the process for the GRT target, but can be adapted to use the ERT target as well.
Note that if you are using Real-Time Workshop 6.1 (R14SP1), see the Related Solution listed at the bottom of the page.
Steps 1 and 2 are configuration steps which you only need to do once.
1. Create the requisite MATLAB tree on the UNIX machine with the source files necessary to compile RTW programs. Copy the following directories from the PC MATLAB installation onto your UNIX machine. (Note: These paths include the source code necessary for Simulink library blocks only.)
simulink/include extern/include rtw/c/grt rtw/c/libsrc rtw/c/src rtw/c/tools
For the purposes of this example, we will assume you installed these files in a directory called:
/home/rtwuser/cross
NOTE: Your make utility and compiler may be sensitive to DOS/UNIX end-of-line character differences. We recommend you also run a dos2unix file conversion utility to ensure your files have UNIX end-of-line characters.
2. Create a cross-platform template makefile on your PC. On the PC, browse to the directory
$MATLABROOT\rtw\c\grt (where $MATLABROOT = the MATLAB root directory on your machine)
Make a copy of the file grt_unix.tmf and name it grt_cross.tmf.
3. On the platform that you will be cross-developing on, run the COMPUTER function in MATLAB.
computer
Take the result of that command and use that value from COMPUTER in the token line. You can also type
help computer
in your PC MATLAB session for a list of legal values. For the purposes of this example, we will assume the cross-development platform is 'SOL2' (Solaris).
4. Open grt_cross.tmf in the MATLAB editor or a text editor so that you can make some changes.
5. Change token line in grt_cross.tmf.
From:
MATLAB_ROOT = |>MATLAB_ROOT<|
To:
MATLAB_ROOT = /home/rtwuser/cross
6. Change token line in grt_cross.tmf:
From:
COMPUTER = |>COMPUTER<|
To:
COMPUTER = SOL2
NOTE: This is assuming the cross-development platform is 'SOL2' (Solaris). This change will hard-code the template makefile to use /home/rtwuser/cross as your MATLAB tree and also hard-codes the template makefile to build on a Solaris machine.
7. Change the following lines to hard-code the makefile to build rtwlib.a.
From:
ifeq ($(OPT_OPTS),$(DEFAULT_OPT_OPTS))RTWLIB = $(MATLAB_ROOT)/rtw/c/lib/$(ARCH)/rtwlib_std.aelseRTWLIB = rtwlib.aendif
To:
#ifeq ($(OPT_OPTS),$(DEFAULT_OPT_OPTS))#RTWLIB = $(MATLAB_ROOT)/rtw/c/lib/$(ARCH)/rtwlib_std.a#elseRTWLIB = rtwlib.a#endif
8. Change the following lines of code under Additional Libraries:
From:
LIBS =|>START_PRECOMP_LIBRARIES<|ifeq ($(OPT_OPTS),$(DEFAULT_OPT_OPTS))LIBS += |>EXPAND_LIBRARY_LOCATION<|/|>EXPAND_LIBRARY_NAME<|_std.aelseLIBS += |>EXPAND_LIBRARY_NAME<|.aendif|>END_PRECOMP_LIBRARIES<| |>START_EXPAND_LIBRARIES<|LIBS += |>EXPAND_LIBRARY_NAME<|.a |>END_EXPAND_LIBRARIES<|LIBS += $(S_FUNCTIONS_LIB) $(INSTRUMENT_LIBS)
To:
LIBS =|>START_PRECOMP_LIBRARIES<|LIBS += |>EXPAND_LIBRARY_NAME<|.a|>END_PRECOMP_LIBRARIES<| |>START_EXPAND_LIBRARIES<|LIBS += |>EXPAND_LIBRARY_NAME<|.a |>END_EXPAND_LIBRARIES<|LIBS += $(S_FUNCTIONS_LIB) $(INSTRUMENT_LIBS)
NOTE: If you are using the ERT target, there will be a few additional lines of code in the section to be removed.
9. On the PC, open your Simulink model and open the Simulation Parameters, Real-Time Workshop tab. Change the template makefile to be 'grt_cross.tmf'.
10. Click the 'Build' button to generate code from your model. You should see RTW generate code and a makefile, but not build the executable.
11. Copy the files from the build directory on your PC to your UNIX working directory:
rtw_proj.tmw
model.h
model_prm.h
model_export.h
model.c
model.mk
model_reg.h
model_common.h
NOTE: Your make of utility and compiler may be sensitive to DOS/UNIX end-of-line character differences. We recommend you also run a dos2unix file conversion utility to ensure your files have UNIX end-of-line characters.
12. In UNIX, browse to the working directory where you copied the RTW model code from step 11.
13. Type the following at your UNIX prompt to build the model:
make -f model.mk
NOTE: You may receive one of the following errors if you did not run a dos2unix utility:
ERROR 1:
make -f vdp.mk : No such file or directory vdp.mk:92: *** commands commence before first target. Stop.
Remedy: run dos2unix utility to conform to UNIX end-of-line character on the files you copied in step 11.
ERROR 2:
make -f vdp.mk cc -c -o vdp.o -O -DMODEL=vdp -DRT -DNUMST=2 -DTID01EQ=1 -DNCSTATES=2 -DUNIX -DMT= 0 -I. -I/home/rtwuser/cross/simulink/include -I/home/rtwuser/cross/extern/in clude -I/home/rtwuser/cross/rtw/c/src -I/home/rtwuser/cross/rtw/c/libsrc vdp.c "/home/rtwuser/cross/simulink/include/simstruc.h", line 47: warning: invalid white space character in directive "/home/rtwuser/cross/simulink/include/simstruc.h", line 48: warning: invalid white
Remedy: This can occur if you did not follow the instructions at the end of step 1. You need to run a dos2unix utility to conform to UNIX end-of-line characters on the files you copied in step 1.
Related Question
- Am I not able to debug an S-function created from a subcomponent in MSVC with the ERT S-Function Target in Real Time Workshop 6.3 (R14SP3)
- Generate DLL from Simulink model
- How to use the Real-Time Workshop generated code in a cross-platform development environment for R12 and R12.1
- Do I receive errors when trying to build the model which has a CAN PCI I/O board using xPC Target 1.1 (R12)
- How to connect 2 generated code from 2 different Simulink models
Best Answer