C++ code importer cannot find the cmath library
Show older comments
Hi, I'm trying to import some C++ controller code I have written to test it with my simulink plant before putting it on hardware. I'm following this tutorial:
When I get to the "Analyze" stage it throws this error:
Error:Failed to parse custom code.
Caused by:
"/Users/alfieeagleton/Desktop/Uni/Thesis/Matlab/cpp/include/actuator.hpp", line 23: error: cannot open source file "cmath"
| #include
| ^
I have included <cmath> and <algorithm> and it will throw an error on both if I remove <cmath>.
Does anyone know what I'm doing wrong?
Thanks
Answers (1)
Adarsh
on 10 Apr 2026 at 5:19
As per my understanding you are encountering an error when using the Simulink Code Importer Wizard to import your C++ controller code. The error message indicates that the parser cannot find the standard C++ library header "cmath" (and "algorithm" if you remove "cmath").
This issue typically occurs when MATLAB does not have the correct compiler configuration for locating C++ standard library headers on macOS.
You can try running the following command in MATLAB to configure your C++ compiler to fix the issue:
mex -setup C++
After running this command, try using the Code Importer Wizard again. This should allow MATLAB to locate the standard library headers automatically.
If the issue persists after running "mex -setup C++", you can manually specify the include paths using a configuration object as show in below example:
cfg = coder.config('lib');
cfg.CustomInclude = {'/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1', '/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include'};
The "CustomInclude" property tells MATLAB where to find the C++ standard library headers.
Alternatively, you can manually add the standard header files to code importer similar to the procedure done in the below MATLAB Answer post:
I hope this helps!
Categories
Find more on C Code Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!