Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
My environment is Ubuntu 12.04, I mostly code in GFortran but occasionally I need to use some C or C++ software. I use g++ compiler for C. This source explains how C routines can be called from Fortran code (page 53). The trouble is it is all predicated on implementation of C 2003 and possibly later versions of Gfortran, as I understand, and although I follow instruction to the letter the simple interface in Gfortran I designed does not compile.
I wonder if anybody has experience working with interoperability and could share the tips.
Distribution: openSuSE Tumbleweed-KDE, Mint 21, MX-21, Manjaro
Posts: 4,588
Rep:
I did some mixing of FORTRAN and C -- about thirty years ago on VAX machines. I had problems too. At long last I found out (in the debugger) that the arguments during function calls were handed over in reverse order. E.g. C-routine(a,b,c) returned its values(!) to main-FORTRAN(c,b,a), i.e. in the wrong variables.
No idea whether that is your problem (I hope not, after thirty years, somebody should have had the leisure to document that) but I wanted to highlight the usefulnes of poking around with a debugger .
I did some mixing of FORTRAN and C -- about thirty years ago on VAX machines. I had problems too. At long last I found out (in the debugger) that the arguments during function calls were handed over in reverse order. E.g. C-routine(a,b,c) returned its values(!) to main-FORTRAN(c,b,a), i.e. in the wrong variables.
No idea whether that is your problem (I hope not, after thirty years, somebody should have had the leisure to document that) but I wanted to highlight the usefulnes of poking around with a debugger .
I have a glimmer of remembering that! Not due to personal experience but once hearing how different languages treat the stack and push/pop. Maybe I'm all wrong, maybe they all treat the stack the same and I'm just confusing that with endian differences.
I subscribed to this wondering who uses FORTRAN still, and why?
It was old when I went to college and we were cringing that there was a course in FORTRAN but then we realized it was for E.C.E. majors not straight EE. So it became a tech elective which we elected not to take.
Unless you have used extern "C" this will be a problem. Interoperability with C++ is much harder. I suggest you avoid g++ and compile with gcc instead.
Quote:
This source explains how C routines can be called from Fortran code (page 53). The trouble is it is all predicated on implementation of C 2003 and possibly later versions of Gfortran,
That's just the gfortran official manual, so you can probably install the version that matches your compiler via the package manager, look for gfortran-doc or similar.
Distribution: openSuSE Tumbleweed-KDE, Mint 21, MX-21, Manjaro
Posts: 4,588
Rep:
Quote:
Originally Posted by rtmistler
I have a glimmer of remembering that! Not due to personal experience but once hearing how different languages treat the stack and push/pop.
Exactly. But those debuggers were fine tools, even then. I also did some C-programming on an Atari ST. You could there manipulate the arguments even after they were pushed onto the stack (verified a nasty error that way), or change values of variables after they were loaded into the registers. Ah, well, Motorola 68.000 processors, who understands Intel .
Quote:
Originally Posted by rtmistler
I subscribed to this wondering who uses FORTRAN still, and why?
Why not? It's a tool, often used in physical sciences. I used it even in a project to create font-editors. Even Cobol is still in use and experts get very good pay to maintain quasi fossilized software.
Quote:
Originally Posted by rtmistler
It was old when I went to college and we were cringing that there was a course in FORTRAN but then we realized it was for E.C.E. majors not straight EE. So it became a tech elective which we elected not to take.
Don't be afraid, I'm right here and it doesn't bite . It's just a language, easy to learn and nice for comparisons of concepts with other languages.
I appreciate all post. First about Fortran. I used Fortran IV, years ago, and now found that modern fortran F90, Fortran 2003 are incredibly advanced over the old versions (pointers, allocatable arrays, etc, etc, etc). I do numerical simulations, it is all physics, the current problem is Fast Fourier Transform and the nice thing about Fortran is that there are thousands programs and routines on the Internet on all aspect of mathematics. Still I found a nice "C" code that works by itself, it compiles and runs, but I need to attach it to the major codes and they are in GFortran.
I never worked on VAX machines, vaguely remember there was a mini mainframe Prime, also DEC production it seems. Nice that my OP provoked some going down a memory lane.
One of the approaches I am pursuing now is to rewrite the C code into modern Fortran. I almost done with it but I still want to crack the problem of interoperability because so much code around is also in C or C++.
Compile errors, Sir. I will try to prepare a better account but in particular the gfortran compiler does not recognize the VALUE attribute. In order to communicate with C one has to write an interface module with declarations of the calling program acceptable to fortran.
Code:
interface
subroutine kronrod ( n, eps, x, w1, w2 ) bind ( c )
use iso_c_binding
integer ( c_int ), VALUE :: n
real ( c_double ), VALUE :: eps
real ( c_double ) :: x(*)
real ( c_double ) :: w1(*)
real ( c_double ) :: w2(*)
end subroutine kronrod
end interface
C passes variables to subroutines by VALUE, Fortran by reference, this is why this VALUE attribute is needed. The compiler gives me an error on this.
Distribution: openSuSE Tumbleweed-KDE, Mint 21, MX-21, Manjaro
Posts: 4,588
Rep:
I -- see. Hmm, I'm used to FORTRAN IV or 77 and the contemporaneous C-version, thus some sort of a fossil . I thought the newer FORTRANs could handle even pointers but I'm way out of my depth here so I'm afraid I won't be much of a help here. Anyway, post away your thoughts, sometimes just formulating them helps in clarifying. What, exactly, is the compiler message?
Sorry for the lack of detail, I am away from my laptop now where most of this software resides. One thing I can mention though. The issue of interoperability has nothing to do with pointers in modern Fortran. Anyway I have Gfortran and I am not sure how much of the latest innovations it has absorbed. The mere fact is that yesterday I finished a program where I used Fortran-90 pointers and allocatable arrays and everything works now without a hitch. Since I did it first time in my life I had a lot of difficulties. Fortran 90 pointers are different from the C pointers. Very interesting, but they do work.
There are two other ways to do dynamic arrays in Fortran 90: automatic arrays and allocatable arrays.
Interoperability with C was introduced in 2003 I think. There is a chance Gfortran does not know about it.
@AlexBB - I have responded to some of your posts over on cplusplus.com regarding the CFFT class. If that is what you are trying to implement as a subroutine in fortran, I have a primitive version working. The fortran program calls the c++ function, passing in a complex array and the array size. The c++ function returns the array and the fortran program prints it out.
My experience with fortran was long ago (think punch cards and Holerith field) so the fortran part is pretty much internet search and brute force.
There were some quirks getting it to compile.
If you think it might help, I can post the code here.
Norm, thank you very much. Your offer is incredible. I sure will be interested. In a way the acuity of the problem faded away. I completely debugged the calling main I wrote to use CFFT, ran it and it worked beautifully. Since I did not have the interoperability (could not figure out how it worked) I finally rewrote the fft.cpp in GFortran and it began working yesterday. It works exactly like the C version.
HOWEVER, I will really appreciate a code sample. It is a great asset. It may serve me well in the future.
To compile: (Note: all the files need to be in the same directory (complex.h, complex.cpp, fft.h, fft.cpp, main.cpp, fortran.f03))
g++ -c *.cpp
gfortran -I/usr/include -c fortran.f03
gfortran -lstdc++ -o application *.o
Problems that I ran into:
gfortran appends an underscore to c/c++ function names (void fft() fails to compile).
Having to put "-I/usr/include" on the gfortran command line is probably just a configuration issue.
Compiling will fail if the fortran file does not have the extension "f03" (for iso_c_binding).
As I said I know little to no fortran but the array printed out agrees with the array that your c++ program outputs.
Norm, thank you. Looks gorgeous! It would have taken me a couple of weeks to work it all out. Will try it later tonight. Perhaps I will have some questions also. Please do not go far away :-)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.