필터 지우기
필터 지우기

Call C/C++ Code from MATLAB Code

조회 수: 1 (최근 30일)
Tareg Mohammed
Tareg Mohammed 2021년 11월 9일
댓글: Tareg Mohammed 2021년 11월 13일
I am trying to implement the following example:
Integrate External Code that Uses Custom Data Types
This example shows how to call a C function that uses data types that are not natively defined within MATLAB®.
consider the MATLAB function addCTypes.m:
function [out] = addCTypes(a,b)
%#codegen
% generate include statements for header files
coder.cinclude('MyStruct.h');
coder.cinclude('createStruct.h');
coder.cinclude('useStruct.h');
% initialize variables before use
in = coder.opaque('MyStruct');
out = 0;
% call C functions
in = coder.ceval('createStruct',a,b);
out = coder.ceval('useStruct',in);
end
The createStruct function outputs a C structure type:
#include <stdio.h>
#include <stdlib.h>
#include "MyStruct.h"
#include "createStruct.h"
struct MyStruct createStruct(double a, double b) {
struct MyStruct out;
out.p1 = a;
out.p2 = b;
return out;
}
The useStruct function performs an operation on the C type:
#include "MyStruct.h"
#include "useStruct.h"
double useStruct(struct MyStruct in) {
return in.p1 + in.p2;
}
To generate code, specify the source (.c) files as inputs:
codegen addCTypes -args {1,2} -report createStruct.c useStruct.c
and I get the follwong result:
------------------------------------------------------------------------
??? Build error: C compiler produced errors. See the Build Log for further details.
More information
Code generation failed: View Error Report
Error using codegen
Error in run (line 1)
codegen addCTypes -args {1,2} -report createStruct.c useStruct.c
should I make a file 'MyStruct.h', and how I should define it ?

답변 (1개)

James Tursa
James Tursa 2021년 11월 12일
편집: James Tursa 2021년 11월 12일
If your code includes a file called MyStruct.h, then yes you should have such a file. I would expect it to be something like this:
/* File: MyStruct.h */
struct MyStruct {
double p1;
double p2;
};
You need the other header files also. These files need to be in a directory that is visible to the compiler when it sees the #include lines.
  댓글 수: 1
Tareg Mohammed
Tareg Mohammed 2021년 11월 13일
I included all relavent files in same directory, with MyStruct.h. and it still shows the following error.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB Algorithm Acceleration에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by