[MATLAB Coder] How to know which libraries to link against when generating C/C++ source files?

조회 수: 4 (최근 30일)
Hello,
I have a question regarding the MATLAB Coder.
I think I got quite good how to use the coder to generate C/C++ source files for different architectures.
However, when, compiling the source codes in an executable or when integrating them in a bigger project, I usually find out that I have to use special flags to tell the compiler it has to include some standard libraries. A trivial example is when the compiler returns an error because the "sqrt" symbol is not defined. The error disappears quickly by adding the -lm flag that includes the math libraries.
Is there a way to discover which libraries need to be linked to the compiled objects? Is there any field in the buildInfo object that can suggest this?

답변 (1개)

Chaitanya
Chaitanya 2023년 7월 11일
In MATLAB Coder, the `buildInfo` object provides information about the build process and allows you to customize the compilation options. However, it does not directly provide information about which standard libraries need to be linked to the compiled objects.
To determine which libraries need to be linked, you can follow these steps:
1. Use the `-v` option during code generation to enable verbose output. This will provide more detailed information about the compilation process.
codegen -v yourFunction
2. Examine the verbose output to identify the compiler commands being executed during the build process. Look for the compiler command that performs the linking step.
... -c yourFile.c -o yourFile.o
... -o yourExecutable yourFile.o -lmath
In this case, the `-lmath` flag indicates that the `libm` library (math library) needs to be linked.
3. Once you identify the required libraries, you can add them to the `buildInfo` object using the `addLinkFlags` method. This will ensure that the necessary libraries are included during the compilation.
cfg = coder.config('lib');
cfg.TargetLang = 'C';
cfg.addLinkFlags('-lm'); % Add the required library flag
codegen -config cfg yourFunction
By examining the verbose output and identifying the libraries needed for linking, you can manually add the required flags to the `buildInfo` object using the `addLinkFlags` method.
Note that the specific libraries required may vary depending on the functions and libraries used in your MATLAB code. It is important to carefully review the verbose output and identify any additional libraries that may need to be linked.
Hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by