Friday, August 24, 2012

Fortran, C++ and qmake

Qt Creator Logo
   Call me a noob, but I love using Qt Creator even when I'm not programming a GUI. The UI greatly facilitates code writing: the syntax highlighting is very customizable, it shows you the methods of a class when you use the . or -> operators, it makes the build process faster and easier to configure (sometimes), etc.

   Now, I didn't want to make this a post about Qt Creator, so here comes the meat. I've been trying to add Fortran code to my numerical library, something I think is pretty common in scientific circles. Of course, that's been done before, but my research has yielded one link for Fortran and Qt. 

   I'm using D.E. Amos' Fortran code to compute Bessel and Hankel functions of complex arguments and all (real) orders (we use identities to convert the negative order computations to positive order ones). Complex orders are not supported. Work by Temme could be used for this, but maybe a later time. 

   I have two Fortran source files that must be incorporated into a C++ numerical library. Using qmake, this is child's play. 

  • First, add the Fortran source files as sources in your .pro file. 
  • SOURCES += machine.for zbesh.for

    Those are the names of the Fortran sources files used by Amos.  
  • Second, create a C++ header file that links to the Fortran subroutines inside the source files. 

    A couple things about this linkage. Notice that I appended a _ to the Fortran subroutine names. The extern keyword tells the compiler that a separately compiled object will be used. By default, g++ will append _ to Fortran subroutines when creating an object file. This means that if you change this compiler setting, be sure to remember to change your function declarations in your C++ header file.
    Also, the functions take pointers to variables. In Fortran, all functions take their arguments by reference. It is thus necessary to use pointers when using Fortran subroutines in C++. 
  • You're done! You can now use your Fortran subroutines in your C++ program! Just don't forget to add the
    when compiling either the program you are writing or a program that uses a library that uses Fortran subroutines.

Wow, that last sentence was terrible.

   Anyway, have fun programming!


  1. Thanks a lot for this example!
    I've been thinking of using QtCreator for Fortran as well, but I'm not compiler-savvy...
    So this will enable me to link Fortran libs to C++ project, right?
    Can I develop a Fortran executable in QtCreator? Will debugging and stuff work?

    Also, FORTRAN code highlighting is great! To enable, go to Tools->Options->Text Editor->Generic Highlighter tab ->Download Definitions. Now, select FORTRAN and "Download Selected".

    1. I'm not Fortran-savvy, but I'll try to answer the best I can.

      From what I see from the Qt Creator docs, I'd say it's entirely possible to create a Fortran executable. GCC knows about Fortran and should compile the main routine properly. As for debugging and stuff, you'll have to configure the kits in Tools->Options->Build & Run-> Kits.

      I'm more hazy on the library side. My assumption is that given a compiled Fortran object, you could write a linkage file as I did in the post above and tell qmake (via your .pro file) to link with this object.

      I have NOT tested any of this, but it shouldn't be too long to do if you have Fortran code somewhere on your machine!

      Thanks for your feedback!

  2. when I run the program in QtCreator appears me an erro saying that the -lgfortran is't found

    1. This might be a stupid question, but are you sure that gfortran is installed and in your library path?

      In some Linux distros, gfortran is offered as a separate package.

  3. There are no stupid question because I am new doing this. About you question: I saw an youtube video about the inataltion of something related to gfortran using MinGW and the creation of the path in the environment variables, and I did that. Other error that appears me when I run the QtCreator program is: there are no rule to make target for "filename.f95" . Can you help me please?

    1. Your problem seems to be with the installation of gfortran rather than with the link with QtCreator. I'd recommend going to StackOverflow, state your problem and share the details of what you've done so far, and other important stuff (like your OS). Share the link here if you please.

  4. I had already asked on StackOverflow, here is the link: My OS is windows 8. And I did, just, what I saw here in your blog and in other question on StackOverflow. If you want more details about what I'm doing or about the error that appears me I can try to explain you. Thanks

  5. Now appears me this error: unrecognized command line option '-W4'

    I think that is refered to this line: SOURCES += src/Test1.for


I'm very sorry to put you through the CAPTCHA. I tried to remove it, but the spam comments were unbelieveable. Here's a funny website to compensate: