Calling C++ functions from MATLAB

2 visualizaciones (últimos 30 días)
Vladimir
Vladimir el 11 de Feb. de 2013
Dear MATLAB users,
I am successfully generating C++ executable from my MATLAB code, however when I try to call cout in order to display the value of a variable, I get "error C3861: 'cout': identifier not found" The line I am using is: coder.ceval('cout',iteration); where iteration is the variable.
I understand, that cout is a function provided with the iostream library. The only reason I can think of for this problem, is MATLAB not calling the iostream library. If this is the case, how to tell explicitly to MATLAB to include the library?
Thank you for time
Vladimir
  1 comentario
Kaustubha Govind
Kaustubha Govind el 11 de Feb. de 2013
How about if you change that to 'std::cout' instead? Also, you may want to create a header foo.hpp with the line:
#include <iostream>
And add foo.hpp to the "codegen" command.

Iniciar sesión para comentar.

Respuesta aceptada

Ryan Livingston
Ryan Livingston el 11 de Feb. de 2013
Please note that
coder.ceval('std::cout',iteration)
will generate
cout(10)
in the C++ code, assuming the value of iteration is 10. However, "std::cout" is not a function and "std::ostream" is not a functor so this line will fail to compile.
Here is an example of how this could be accomplished:
function callCout()
% Call << on cout. Don't forget to null-terminate the string
coder.ceval('std::operator<<',cout(),['Hello world!' char(0)]);
% Call endl
coder.ceval('endl', cout());
function os = cout
% Returns an opaque representing std::cout and inlines the value where needed
coder.inline('always');
os = coder.opaque('std::ostream', 'std::cout', 'HeaderFile', '<iostream>');
then compile with:
c = coder.config('mex');
c.CustomHeaderCode = '#include <iostream>';
c.TargetLang = 'C++';
codegen callCout -config c
and call the generated MEX
callCout_mex()

Más respuestas (1)

Vladimir
Vladimir el 12 de Feb. de 2013
Thank you both for the responses. In the end, I found it easier to call the printf command with coder.ceval. I inserted the header stdio.h to my code and that completed the task.
Thanks again.
vladimir
  1 comentario
Ryan Livingston
Ryan Livingston el 12 de Feb. de 2013
Glad to hear that you found a solution.

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB Coder en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by