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

조회 수: 20 (최근 30일)
German
German 2025년 12월 16일
답변: Ayush 2025년 12월 18일
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일
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개)

카테고리

Help CenterFile Exchange에서 Generating Code에 대해 자세히 알아보기

제품


릴리스

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by