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

17 vues (au cours des 30 derniers jours)
German
German le 16 Déc 2025
Réponse apportée : Ayush le 18 Déc 2025
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 commentaires
Raghu Boggavarapu
Raghu Boggavarapu le 16 Déc 2025
Modifié(e) : Raghu Boggavarapu le 16 Déc 2025
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 le 17 Déc 2025
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.

Connectez-vous pour commenter.

Réponse acceptée

Ayush
Ayush le 18 Déc 2025
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!

Plus de réponses (0)

Catégories

En savoir plus sur Generating Code dans Help Center et File Exchange

Produits


Version

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by