How to include all required headers in code generated by MATLAB coder?

39 views (last 30 days)
I'm generating C++ source code from MATLAB with Coder.
Some of my functions require cs.h: #include "cs.h" but MATLAB does not automatically include the "cs.h" header file in the generated files. I can find the file from ${MATLAB_ROOT_DIR}/toolbox/eml/externalDependency/CXSparse/Include, but I do not want to manually search for the file and include it. I later manually compile the generated C++ and at the moment, I have to manually find the "cs.h" file and configure it to the build. I do not want to do this everytime I get a new dependency.
I would like to have all the headers and sources that the generated C++ code needs automatically in the generated codegen folder. Is this possible?
  2 Comments
Denis Gurchenkov
Denis Gurchenkov on 30 Sep 2022
Hi Kyösti,
The C++ code generated by MATLAB Coder always includes all the required headers. And in particular, if the generated code uses CXSparse library, then the cs.h file would get automatically included in all the generated .cpp files where it is needed. If this is not the case, if you see a generated .cpp file that fails to compile because it needsa the cs.h file but there is no #include, this may be a bug in the tool. Please either add reproduction steps here, or contact MathWorks technical support, so that we can investigate what is going on.
If the generated code contains the #include statement, and your concern is that you need to manually copy the .h file or add the include path, then you can instead use the "package" step in the MATLAB Coder UI, or the packNGo function at the command line as to package all the generated code, along with its dependencies (.h files, lib files and so on) into a single archive. Then, all the files would be in one folder and you will not need to use any custom include paths or search for files.
If the above information is not helpful, please feel free to add more details so we can diagnose the issue. If your example is sensitive and you are not at liberty of discussing details here, please feel free to contact MathWorks technical support so we can help you.
Thanks,
Denis.
Hein Gustavsen
Hein Gustavsen on 9 Nov 2022
We have the same problem with MATLAB R2022b update 1 and MATLAB Coder 5.5. Files are generated correctly, but The files from CXSparse and CXSparseHelper are not copied to output. Work-around is to manually coping the files from those folders.

Sign in to comment.

Answers (1)

Ryan Livingston
Ryan Livingston on 9 Nov 2022
Edited: Ryan Livingston on 9 Nov 2022
When I run the codegen command in MATLAB Coder in MATLAB R2022b, I see that all CXSparse files appear in the codegen folder under CXSparse:
>> !tree codegen/lib/f/CXSparse/
codegen/lib/f/CXSparse/
├── CXSparseSupport
│   ├── coder_cs.h
│   ├── makeCXSparseMatrix.c
│   ├── makeCXSparseMatrix.h
│   ├── solve_from_lu.c
│   ├── solve_from_lu.h
│   ├── solve_from_qr.c
│   ├── solve_from_qr.h
│   ├── unpackCXStruct.c
│   └── unpackCXStruct.h
├── Include
│   └── cs.h
├── License
│   ├── ChangeLog
│   ├── lesser.txt
│   └── License.txt
├── Source
│   ├── cs_ci_bundle.c
│   └── cs_ri_bundle.c
└── SuiteSparse_config
├── Makefile
├── README.txt
├── SuiteSparse_config.c
├── SuiteSparse_config.h
└── SuiteSparse_config.mk
5 directories, 20 files
I verified that this list is complete by zipping up the folder and building the generated code on another machine. If you see other behavior with MATLAB Coder, can you please share reproduction steps with technical support so we could investigate?
As Denis mentioned, in general the best practice to package the generated code and all dependencies is by using one of the following:
  • The MATLAB function packNGo: packNGo('/some/folder/codegen/lib/foo')
  • The package argument to codegen: codegen foo.m -package fooPackage.zip
  • The package button in the MATLAB Coder App and report
Those will give you a ZIP file with all dependencies included that can be relocated.

Categories

Find more on Get Started with MATLAB Coder in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!