How to pass an argument by reference to a C++ Interface library function?
조회 수: 11 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2023년 3월 3일
답변: MathWorks Support Team
2023년 3월 3일
I have the following C++ header file.
// sample header.hpp
#include <vector>
// fills in the input vector
void getData(std::vector<char>& arr) {
char data [] = {'M', 'A', 'T', 'L', 'A', 'B', '\0'};
arr.clear();
for (auto c : data) {
arr.push_back(c);
}
}
The argument for the getData function needs to be passed by reference. How can I do that with the C++ Interface?
채택된 답변
MathWorks Support Team
2023년 3월 3일
This example shows how you can pass arguments by reference with the C++ Interface.
Generate the "header-only" library definition and specify the library name as lib.
>> clibgen.generateLibraryDefinition("header.hpp","PackageName=lib" )
Build the interface and add the interface folder to the MATLAB path, where
is the full path to the lib folder.
>> build(definelib)
>> addpath('<path-to-library>\lib')
Now, you can use the library and pass by reference in the following way.
>> arr = clib.array.lib.Char(1); % array of size 1
>> clib.lib.getData(carr); % fill in carr by calling the function
>> char(carr.int8) % convert data into MATLAB
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Call C++ from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!