This example shows how to call a generated C library function
from C code. It uses the C static library function absval
described
in Call a C/C++ Static Library Function from MATLAB Code.
Write a main
function in C that
does the following:
Includes the generated header file, which contains the function prototypes for the library function.
Calls the initialize function before calling the library function for the first time.
Calls the terminate function after calling the library function for the last time.
Here is an example of a C main
function that
calls the library function absval
:
/* ** main.c */ #include <stdio.h> #include <stdlib.h> #include "absval.h" int main(int argc, char *argv[]) { absval_initialize(); printf("absval(-2.75)=%g\n", absval(-2.75)); absval_terminate(); return 0; }
Configure your target to integrate this custom C main function with your generated code, as described in Specify External File Locations.
For example, you can define a configuration object that points to the custom C code:
Create a configuration object. At the MATLAB® prompt, enter:
cfg = coder.config('exe');
Set custom code properties on the configuration object, as in these example commands:
cfg.CustomSource = 'main.c'; cfg.CustomInclude = 'c:\myfiles';
Generate the C executable. Use the -args
option
to specify that the input is a real, scalar double. At the MATLAB prompt,
enter:
codegen -config cfg absval -args {0}
Call the executable. For example:
absval(-2.75)