having a hard time with codegen, even though interactive coder works fine

21 views (last 30 days)
While in an interactive matlab session, when I export *.m scripts to C sources and build, I do get an executable and it actually runs correctly; when I attempt to do the same thing programmatically with codegen, with another matlab script that I run with matlab in batch mode, I do get an executable, but it does not run, I just get "Bus error (core dumped)".
May I get some assistance on what matlab commands needed and where to place them?
Here are a couple of considerations:
- I provide my own main.c
- I use a dynamic library of mine that is somewhere else
Here is the script I am passing to matlab in batch mode:
function codegen_exec()
global libname_path
global libname_ext
libname_path = '/path/to/mylib/parentDir';
libname_ext = '.so';
addpath(genpath(libname_path));
entry_point_function = 'Main.m'
cfg = coder.config('exe');
cfg.TargetLang = 'C';
cfg.TargetLangStandard = 'C99 (ISO)';
cfg.CustomSource = 'main.c';
example_filename = "Input.in";
t = coder.typeof(example_filename);
t.StringLength = Inf;
input_args = {t};
codegen(entry_point_function, ...
'-config', cfg, ...
'-globals', {'libname_path', libname_path, 'libname_ext', libname_ext}, ...
'-args', input_args);
end
One think I did interactively is to provide matlab coder with a test script 'launch_Main.m', that runs 'Main.m'. I don't know how to that programmatically.
What am I missing? All hints will be very much appreciated.
  2 Comments
Raghu Boggavarapu
Raghu Boggavarapu on 16 Dec 2025 at 11:25
Edited: Raghu Boggavarapu on 16 Dec 2025 at 11:26
Are you loading any shared library from the libname_path ? How are you loading the library from the path ?
If possible could you share your main.c
Interactive session creates a MEX to ensure that generated code works correctly with your test bench. You can generate MEX and use coder.runTest to reproduce the results: please see the documentation here: coder.runTest - Run test replacing calls to MATLAB functions with calls to MEX functions - MATLAB
German
German on 17 Dec 2025 at 2:33
Thanks for the hint.
I am not the actual matlab user, I am just helping a designer. Browsing around the matlab UI, I discover that it can export the coder project file to a script file; so, I did that and it totally help....I just had to do was add the path to my external library.

Sign in to comment.

Accepted Answer

Ayush
Ayush on 18 Dec 2025 at 6:03
Hi German,
The main cause seemed to be that the path to your external library wasn't properly set in your batch script. Exporting the Coder project to a script and explicitly adding the library path before running codegen solved the problem.
Glad it worked out!

More Answers (0)

Categories

Find more on Generating Code in Help Center and File Exchange

Products


Release

R2025a

Community Treasure Hunt

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

Start Hunting!