Including Partial Paths to C++ Header Files with Legacy Code Tool
9 views (last 30 days)
Show older comments
Hello,
I have a problem with getting the legacy code tool to work properly. I have a C++ codebase with the following file structure:

Note that my_lib is inside my working directory. I am using the Legacy Code Tool to create a Simulink function that calls my_lib/src/my_func.cpp. The function has the following include directives:
#include "my_lib/my_func.hpp"
If this was soley a C++ project, I would create a CMakeLists.txt file with the "my_lib/include/" directory to get the compiler to understand where to look for these header file. I have attempted to provide a similar thing to the Legacy Code Tool via the following commands:
def = legacy_code('initialize');
% add the include and source paths
lib_name = 'my_lib';
incPath = [lib_name '/' 'include'];
srcPath = [lib_name '/' 'src'];
def.IncPaths = {incPath};
def.SrcPaths = {srcPath};
% add the include and source files
def.SourceFiles = {'my_func.cpp'};
def.HeaderFiles = {'my_func.hpp'};
% name the SFunction specs (used by simulink)
def.SFunctionName = 'sfun_my_func';
def.OutputFcnSpec = 'single y1 = my_func(single u1, single u2, single u3)';
def.Options.language = 'C++';
% generate the mex function and compile the code
legacy_code('sfcn_cmex_generate', def);
legacy_code('compile', def);
However, this fails due to the fact that the resulting "sfun_my_func.cpp" file is created in the working directory with the header file #include "my_func.hpp" rather than the (expected) #include "my_lib/include/my_lib/my_func.hpp".
With all of this in mind, how can I get the Legacy Code Tool to work without changing the filestructure nor the include directives in my_func.cpp?
Thank you in advance for your time and I look forward to anyone's advice on this matter.
0 Comments
Answers (1)
Varun
on 22 Aug 2023
Edited: Varun
on 22 Aug 2023
Hi Justin,
I understand that "my_func.hpp" is in directory "my_lib/include/my_lib/" and still generated file "sfun_my_func.cpp" has included this header as #include "my_func.hpp" rather than #include "my_lib/include/my_lib/my_func.hpp".
Actually, #include "my_func.hpp" is the correct because you have already defined as:
incPath = [lib_name '/' 'include'];
def.IncPaths = {incPath};
And when mex compiler will compile the sfun_my_func.cpp, “my_lib/include/my_lib/" will be added into the the list of directories to search for header files by “legacy code tool”, so no need to use #include "my_lib/include/my_lib/my_func.hpp" and #include "my_func.hpp" is enough.
You can also notice this on command window as:
### Start Compiling sfun_my_func.cpp
mex(‘–I<absolute path of my_lib/include/my_lib/>’,...)
0 Comments
See Also
Categories
Find more on Simulink Coder 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!