Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. [SOLVED] Compiling Fortran code with gfortran within a C++ project
QtWS25 Last Chance

[SOLVED] Compiling Fortran code with gfortran within a C++ project

Scheduled Pinned Locked Moved Installation and Deployment
7 Posts 2 Posters 12.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    valandil211
    wrote on last edited by
    #1

    Hi everyone!

    I'm working on a numerical library with Qt Creator (a C++ project using qmake). As it is with numerical projects, I have some Fortran code that I want to use in the project. I have .for files that are (currently) compiled with g++ and a header that links those objects to the C++ code.

    However, when I try to actually use those functions, the compiler complains about undefined reference to gfortran... . Now, I'm pretty sure this comes from bad linking at compile time.

    The question is: how can I use gfortran to compile the .for files and g++ to compile the .cpp files? Can I define a new variable in qmake, a new build step in the Qt Creator interface?

    Anyhow, thanks for your time,

    Joey Dumont

    1 Reply Last reply
    1
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      Check out qmake's QMAKE_EXTRA_COMPILERS

      I don't have a link handy, but Google can tell you more.

      There's also the ability to add custom build steps under the Build Settings in Qt Creator.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • V Offline
        V Offline
        valandil211
        wrote on last edited by
        #3

        Yes, I tried the former:
        @
        FORTRAN_SOURCES +=
        machine.for
        zbesh.for \

        fortran.output = ${QMAKE_FILE_BASE}.o
        fortran.commands = gfortran ${QMAKE_FILE_NAME} -o ${QMAKE_FILE_OUT}
        fortran.input = FORTRAN_SOURCES
        QMAKE_EXTRA_COMPILERS += fortran@

        Based on a "document":http://qt-project.org/doc/qt-4.8/qmake-environment-reference.html#customizing-makefile-output about qmake. However, I get

        @undefined reference to `main'@

        when I try to build this. I think that's because gfortran thinks I'm compiling an executable, while I'm compiling a static library. But qmake knows that:
        @TEMPLATE = lib
        CONFIG += staticlib@

        Can I pass those options to the custom compiler? There seems to be a CONFIG field for the custom compiler, but I'm not sure of the syntax.

        As for the latter, adding custom build steps to compile only certain files seems complicated.

        Joey Dumont

        1 Reply Last reply
        0
        • V Offline
          V Offline
          valandil211
          wrote on last edited by
          #4

          Or, as "this":http://stackoverflow.com/questions/5663083/linking-fortran-and-c-binaries-using-gcc suggests, could I pass the option -lgfortran selectively to the Fortran source files?

          Joey Dumont

          1 Reply Last reply
          0
          • V Offline
            V Offline
            valandil211
            wrote on last edited by
            #5

            Or "this":http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.html, which suggest to use
            @f77 -c testF.f
            g++ -c testC.cpp
            g++ -o test testF.o testC.o -lg2c@

            but how to make the last line happen automatically? --

            Joey Dumont

            1 Reply Last reply
            0
            • V Offline
              V Offline
              valandil211
              wrote on last edited by
              #6

              Nevermind. I am just plain stupid. In the program that uses the library, I forgot to add the line
              @LIBS += -lgfortran@

              In the library .pro file, one should just list the Fortran source files in the SOURCES. qmake will take care of the rest. The -lgfortran lib is also unnecessary for the library.

              Joey Dumont

              1 Reply Last reply
              1
              • M Offline
                M Offline
                mlong
                wrote on last edited by
                #7

                Ah, very nice! Good to know! Glad you got it solved!

                Software Engineer
                My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved