1

I am using Matlab Coder to generate C code to be used as a subsystem of a larger C project. A first attempt at a code generation command would be

codegen -config cfg -o ...

where cfg is the name of a coder.CodeConfig object and ... denotes that a list of functions and their argument specifications follows. The issue with this command is that it does not automatically generate the dependency tmwtypes.h. I assume this is not an issue if one uses Matlab to compile the code, but I copy the generated files to the main project and build directly with gcc, so I need that dependency to be included.

It turns out that the package option can help. If I generate with

codegen -config cfg -package pkg -o ...

then a zip file pkg.zip is generated, containing the generated code with the dependency tmwtypes.h included. I can unzip this in the main project and compile; it works! Apparently the packNGo function can be used to obtain similar results; in fact it seems likely that codegen uses packNGo to implement the package option.

But this seems so silly! I shouldn't need to generate a zip file just to get the correct dependencies. There has to be a better way, right? So what is the proper way to generate the code and make sure that tmwtypes.h is included? I suppose I could just copy it into the folder as part of my script, but the default location of tmwtypes.h will be different on different machines, so that has its own set of issues. Matlab is clearly capable enough to detect and include the dependency, since it is included when using the package option.

2
  • Why do you feel that using -package / packNGo (they are the same thing under the hood) is unnecessary? If you already are using a script to copy the requisite header files into your main project, could you not just as easily have your script extract all the generated code and dependencies from the packaged zip file? -package is the recommended workflow because it will gather all of the dependencies. So if you update your project in such a way that the generated code depends on a new header file, -package will pull it in automatically. Commented May 2, 2024 at 16:21
  • Well, that is actually what I am doing at the moment (despite my earlier answer below). I guess it just felt a bit strange and unintuitive to have to create a zip file to get the dependencies. The zipping and unzipping has no point (in my usecase). And it requires additional logic in the codegen and build script to unzip and delete the zip file, which I would have preferred to be able to avoid. Commented May 3, 2024 at 11:56

1 Answer 1

1

According to this it seems like I can get the platform dependent path to tmwtypes.h by using the matlabroot command:

matlabroot + "/extern/include/tmwtypes.h"

I can use this to copy tmwtypes.h into the main project, as part of my script for code generation and compilation.

I would still appreciate information about a better solution, if there is one.

Sign up to request clarification or add additional context in comments.

2 Comments

This is a good solution, I don’t know what you would consider better. The header files are in the same directory within the MATLAB installation for every platform and have been there since the very first version of MATLAB I used, maybe 25 years ago. They’re not going to change that.
Thank you. It is good to know that the location of the header files has been stable for so long. Then I feel fine using this solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.