Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28543 Discussions

How to call C++ cout statement in Fortran

rakeshthp
Beginner
948 Views
Hello,

I am new to FORTRAN.. And have knowledge of nothing.. Basically what i want to do is that, i want to redirect the output from console to a widget say a textbox.. In C++ i am able to do it.. Now i have a part of my project in FORTRAN... And there are some print statements in it.. So I want to redirect the output of this code to the textbox..

So what i thought is that, anyhow it is working with C++'s cout statement, why cant we replace fortran's print statement by C++'s cout statement..

I just want to know is it directly or indirectly possible to print this output using

std::cout

stmt in FORTRAN Code..?? I mean to say, to print the output to the console using C++'s cout statement??

If so, how it can be done?? I want to display output of fortran code into a textbox and my application is written in C++..

Or is there any alternative solution to implement this..??

Please share your ideas and comments.

Thanks in advance
0 Kudos
6 Replies
Steven_L_Intel1
Employee
948 Views
You cannot call C++'s "cout" from fortran. You can call a C++ routine that does what you want - I think this is the best approach.
0 Kudos
rakeshthp
Beginner
948 Views
Hello Steve,

Thanks for your quick reply.. You mean to say that, i have to write a C++ function which contains my cout statement.. and then call this routine from FORTRAN..?? If so, then can just give me a hint, as to how to go about it.?? I am new to FORTRAN and just know some basic things.. How can I call this C++ functions in FORTRAN code..

I mean, there are classes and objects in C++ .. So, how to do this.?

Thanks in advance
0 Kudos
Steven_L_Intel1
Employee
948 Views
Here's a simple example:

C++ code:

[cpp]#include 

extern "C" void cout_sub (char * string) {
std::cout << string;
}[/cpp]

Fortran code:

[fortran]program cout_test
use iso_c_binding
implicit none

interface
subroutine cout_sub (string) bind(C)
character, dimension(*) :: string
end subroutine cout_sub
end interface

character(100) string
integer i

! As an example, write the integers 1 to 10 in a loop
do i=1,10
! Convert the number to a string and add a trailing newline (n) plus a NUL termination
write (string,'(I0,A)') i,C_NEW_LINE//C_NULL_CHAR
! Call C++ to write the text
! Note that Fortran allows a character string to be passed to an
! array of single characters. The NUL is needed because no length is passed.
call cout_sub (string)
end do
end[/fortran]
0 Kudos
jimdempseyatthecove
Honored Contributor III
948 Views
Read the section in the IVF documents relating to Interoperability with C/C++. This provides you with examples of how to make simple calls. Once you can write a simple function in C++ to receive arguments from a Fortran subroutine call then you are all set to write your support routines. Experiment and learn how and whento pass argumentsby value and by reference.

Once you are comfortable with this then look at the IVF documents relating to Generic Interfaces. By using Generic Interfaces you can then write Fortran code that "approximates" the cout.

trash = cout("The answer is: ") + cout(Array(i)) + cout(" at ") + cout(i) + cout(endl)


You create a generic function returning a trash integer (or maybe the column position or print width). The generic function interface takes the different argument types and then calls the appropriate stub function in C++.

Once the above works, you can experiment with adding the formatting statements to the cout.

Jim Dempsey

0 Kudos
mflemming
Beginner
948 Views

If you just want to write to stdout, a

write(*,*) whatever

is all that is needed.

0 Kudos
jimdempseyatthecove
Honored Contributor III
948 Views
Which is truewhenonly the Fortran portion of the code performs the output.

When the application is a combination of Fortran and C++ it should be noted that a common practice for implementing cout is to buffer the data, and in some cases format and/or output the data by way of a buffer and, depending on implementation, by use of a non-applicationvisiblehelper thread.When this is the case, then the two ways for the data to be properly sequenced is for the Fortran side to issue calls to cout to output its data, or for the Fortran side to call the C++ size to flush() the cout data prior to the Fortran output and following the Fortran output, issue the Fortran equivalent to flush. Failure to do so will end up with interleaved output.

Jim Dempsey
0 Kudos
Reply