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

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 件のコメント

Raghu Boggavarapu
Raghu Boggavarapu 2025 年 12 月 16 日
編集済み: Raghu Boggavarapu 2025 年 12 月 16 日
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 2025 年 12 月 17 日
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.

サインインしてコメントする。

 採用された回答

Ayush
Ayush 2025 年 12 月 18 日

0 投票

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!

その他の回答 (0 件)

カテゴリ

製品

リリース

R2025a

質問済み:

2025 年 12 月 16 日

回答済み:

2025 年 12 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by