Call a C function from Matlab
Ältere Kommentare anzeigen
Hello,
I am trying to call a C function form Matlab. The files are in different folders. How can I achieve that? I am using Matlab R2018b.
The C file is called main.c and its funtion to be called is int main(void). I tried something like this:
% callmain.m
function y = callmain() %#codegen
y = 0;
coder.cinclude('main.h');
y = coder.ceval('main');
end
With the following code I tried to specifiy the location of the files:
cfg = coder.config('lib');
cfg.CustomInclude = [fullfile(pwd,'Inc') ' ' fullfile(pwd,'Src')];
cfg.CustomSource = fullfile(pwd,'Src','main.c');
This line of code is supposed to generate the code:
codegen -config cfg callmain.m
The complete C code can be obtained from the following link:
I would appreciate your help!
Thanks
17 Kommentare
Darshan Ramakant Bhat
am 3 Mär. 2020
Make sure that you have "main.h" and "main.c" files present in the "CustomInclude" directory.
In your codebase I dont see those files inside pwd/Src or pwd/Inc folders.
Sebastian Linn
am 3 Mär. 2020
Bearbeitet: Sebastian Linn
am 3 Mär. 2020
Darshan Ramakant Bhat
am 4 Mär. 2020
I think the problem is there in the way you are including the multiple paths in "cfg.CustomInclude". Please refer to the correct syntax from the below documentation :
Specifically you have to use "pathsep" command to seperate different paths. Make sure that you have included all the relevent directories.
Hope this will be helpful for you.
Darshan Ramakant Bhat
am 6 Mär. 2020
Are you able to resolve the issue ?
Sebastian Linn
am 6 Mär. 2020
Darshan Ramakant Bhat
am 6 Mär. 2020
Can you please state the specific error that you are getting ?
Sebastian Linn
am 6 Mär. 2020
Darshan Ramakant Bhat
am 6 Mär. 2020
From the error it is clear that it is not able to find the "stm32f4xx_hal.h" header file. You need to include all the directories of the required headerfiles properly. Something like below :
cfg.CustomInclude = [fullfile(pwd,'Inc') pathsep fullfile(pwd,'Src') pathsep fullfile(pwd,'Drivers','STM32F4xx_HAL_Driver','Inc') pathsep fullfile(pwd,'Drivers','CMSIS','Device','ST','STM32F4xx','Include') pathsep fullfile(pwd,'Drivers','CMSIS','Include')];
Sebastian Linn
am 7 Mär. 2020
Prasanna Gude
am 9 Mär. 2020
As I don't have access to the '\LED\codegen\lib\callmain\examples', I am not able to resolve your issue.
Sebastian Linn
am 9 Mär. 2020
Prasanna Gude
am 9 Mär. 2020
Try building 'mex' function, instead of calling c function from matlab.
Sebastian Linn
am 9 Mär. 2020
Prasanna Gude
am 11 Mär. 2020
When I am trying to build mex, I am getting an error like this: error: unknown type name 'TIM_HandleTypeDef'
Did you defined 'TIM_HandleTypeDef' anywhere?
Sebastian Linn
am 11 Mär. 2020
Prasanna Gude
am 11 Mär. 2020
'TIM_HandleTypeDef' is there in the third party library "stm32f4xx_hal.h". Did you build that library? If not build that library and after that try building 'mex'.
Sebastian Linn
am 12 Mär. 2020
Antworten (1)
Pankhuri Kasliwal
am 20 Aug. 2020
0 Stimmen
Kategorien
Mehr zu Introduction to Installation and Licensing finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

