Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Compiling static Fortran library Qt
Forum Updated to NodeBB v4.3 + New Features

Compiling static Fortran library Qt

Scheduled Pinned Locked Moved Unsolved Language Bindings
10 Posts 5 Posters 2.5k Views 4 Watching
  • 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.
  • J Offline
    J Offline
    Jakub Srna
    wrote on 9 Apr 2019, 22:24 last edited by
    #1

    Hello everyone,
    first I would like to say, that I am pretty new to the Qt, so please don't be harsh on me. I have Fortran files ( both header and source ) which I would like to compile to the static library. This library will be then used by the rest of the code.
    Structure of the whole code looks like this:

    pro file
    model folder
       -pri file
    view folder
       -pri file
    controller folder
       -pri file
    fortran folder
       -pri file
    

    where the last should be the static library. When I try to compile the static library I get the linker error 1104. The pri file looks as follows.

    TEMPLATE = lib
    CONFIG += staticlib
    LIBS += lgfortran
    
    HEADERS += \
        $$PWD/CPA_ALL.INC \
        ...
    
    SOURCES += \
        $$PWD/Auxiliary.for \
        ...
    

    And in the pro file I simply use include(Fortran/fortran.pri). I know that's probably completely wrong but I did not find any source which would push me a bit forward. Thank you for any suggestions.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 10 Apr 2019, 06:28 last edited by
      #2

      qmake supports only C++. Build your fortran code using different build system, then in Qt app you can include the resulting library.

      (Z(:^

      K 1 Reply Last reply 10 Apr 2019, 06:41
      0
      • S sierdzio
        10 Apr 2019, 06:28

        qmake supports only C++. Build your fortran code using different build system, then in Qt app you can include the resulting library.

        K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 10 Apr 2019, 06:41 last edited by
        #3

        @sierdzio said in Compiling static Fortran library Qt:

        qmake supports only C++.

        Not strictly true. You can compile and link fortran, but it's a pain. You need to configure an extra compiler in the project file.

        Build your fortran code using different build system, then in Qt app you can include the resulting library.

        Probably a painless solution, indeed.

        Read and abide by the Qt Code of Conduct

        S 1 Reply Last reply 10 Apr 2019, 06:43
        3
        • K kshegunov
          10 Apr 2019, 06:41

          @sierdzio said in Compiling static Fortran library Qt:

          qmake supports only C++.

          Not strictly true. You can compile and link fortran, but it's a pain. You need to configure an extra compiler in the project file.

          Build your fortran code using different build system, then in Qt app you can include the resulting library.

          Probably a painless solution, indeed.

          S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 10 Apr 2019, 06:43 last edited by
          #4

          @kshegunov said in Compiling static Fortran library Qt:

          You need to configure an extra compiler in the project file.

          Oh right, that could work, I haven't thought about it. @Jakub-Srna here is the documentation for extra compilers feature https://doc.qt.io/qt-5/qmake-advanced-usage.html#adding-compilers

          (Z(:^

          1 Reply Last reply
          3
          • J Offline
            J Offline
            Jakub Srna
            wrote on 10 Apr 2019, 10:55 last edited by
            #5

            Thank you very much, both of you. So I made a bit of googling and got to a point, where I get this

            'gfortran' is not recognized as an internal or external command,
            operable program or batch file.
            

            I know what it means, just I don't know why I get it.

            TEMPLATE = lib
            CONFIG += staticlib
            LIBS += -lgfortran
            
            gfortran.input = SOURCES \
                             HEADERS
            gfortran.commands = gfortran $${FORTRAN_FLAGS} ${QMAKE_FILE_NAME} -c -o ${QMAKE_FILE_OUT}
            gfortran.output = ${QMAKE_FILE_BASE}.o
            
            QMAKE_EXTRA_COMPILERS += gfortran
            
            HEADERS += \
                $$PWD/CPA_ALL.INC \
                ....
            
            SOURCES += \
                $$PWD/Auxiliary.for \
                ....
            

            This is the updated version of the pri file. I am a bit confused as the library -lgfortran is apparently included as it should but it's not recognized when it should. Any ideas how should I continue? I am on Windows machine MSVC 2017.

            K K 2 Replies Last reply 10 Apr 2019, 14:56
            0
            • J Jakub Srna
              10 Apr 2019, 10:55

              Thank you very much, both of you. So I made a bit of googling and got to a point, where I get this

              'gfortran' is not recognized as an internal or external command,
              operable program or batch file.
              

              I know what it means, just I don't know why I get it.

              TEMPLATE = lib
              CONFIG += staticlib
              LIBS += -lgfortran
              
              gfortran.input = SOURCES \
                               HEADERS
              gfortran.commands = gfortran $${FORTRAN_FLAGS} ${QMAKE_FILE_NAME} -c -o ${QMAKE_FILE_OUT}
              gfortran.output = ${QMAKE_FILE_BASE}.o
              
              QMAKE_EXTRA_COMPILERS += gfortran
              
              HEADERS += \
                  $$PWD/CPA_ALL.INC \
                  ....
              
              SOURCES += \
                  $$PWD/Auxiliary.for \
                  ....
              

              This is the updated version of the pri file. I am a bit confused as the library -lgfortran is apparently included as it should but it's not recognized when it should. Any ideas how should I continue? I am on Windows machine MSVC 2017.

              K Offline
              K Offline
              koahnig
              wrote on 10 Apr 2019, 14:56 last edited by
              #6

              @Jakub-Srna

              Personally I would go for a painlesser option as already suggested above. Make a library and use it for linking with C/C++.

              Anyway probably gfortan command cannot be found because it is not known in the environment. You can set the project's build environment under "Projects"->"Build Settings". On this page at the end is the "Build environment". Typically there are the settings copied from System's environment, but you can add there what is required.

              Certainly you could also simply spoil your system's environment by additional settings and path extensions. However, extending the build environment keeps things cleaner.

              Vote the answer(s) that helped you to solve your issue(s)

              P 1 Reply Last reply 10 Apr 2019, 15:33
              0
              • K koahnig
                10 Apr 2019, 14:56

                @Jakub-Srna

                Personally I would go for a painlesser option as already suggested above. Make a library and use it for linking with C/C++.

                Anyway probably gfortan command cannot be found because it is not known in the environment. You can set the project's build environment under "Projects"->"Build Settings". On this page at the end is the "Build environment". Typically there are the settings copied from System's environment, but you can add there what is required.

                Certainly you could also simply spoil your system's environment by additional settings and path extensions. However, extending the build environment keeps things cleaner.

                P Offline
                P Offline
                Pablo J. Rogina
                wrote on 10 Apr 2019, 15:33 last edited by
                #7

                @Jakub-Srna

                Personally I would go for a painlesser option as already suggested above. Make a library and use it for linking with C/C++.

                As @koahnig and others mention, I guess if that you're able to build a library for use with C/C++ with your Fortran code externally to Qt, it would save you time and effort...

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • J Jakub Srna
                  10 Apr 2019, 10:55

                  Thank you very much, both of you. So I made a bit of googling and got to a point, where I get this

                  'gfortran' is not recognized as an internal or external command,
                  operable program or batch file.
                  

                  I know what it means, just I don't know why I get it.

                  TEMPLATE = lib
                  CONFIG += staticlib
                  LIBS += -lgfortran
                  
                  gfortran.input = SOURCES \
                                   HEADERS
                  gfortran.commands = gfortran $${FORTRAN_FLAGS} ${QMAKE_FILE_NAME} -c -o ${QMAKE_FILE_OUT}
                  gfortran.output = ${QMAKE_FILE_BASE}.o
                  
                  QMAKE_EXTRA_COMPILERS += gfortran
                  
                  HEADERS += \
                      $$PWD/CPA_ALL.INC \
                      ....
                  
                  SOURCES += \
                      $$PWD/Auxiliary.for \
                      ....
                  

                  This is the updated version of the pri file. I am a bit confused as the library -lgfortran is apparently included as it should but it's not recognized when it should. Any ideas how should I continue? I am on Windows machine MSVC 2017.

                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 11 Apr 2019, 09:11 last edited by kshegunov 4 Nov 2019, 09:13
                  #8

                  @Jakub-Srna said in Compiling static Fortran library Qt:

                  I am a bit confused as the library -lgfortran is apparently included as it should but it's not recognized when it should. Any ideas how should I continue?

                  The error is about your compiler, not so much about the library. What you need to do is to have a separate variable, separate from SOURCES and HEADERS that is, which lists your fortran source code files, and which you pass as input to the compiler. Also, and I cite from memory here, you need to "trick" qmake to add the generated object files from the extra compiler to the already existing list of object files, so linking to go as expected. Unfortunately I couldn't find an example to give you, I must've deleted it or put it somewhere, so I have to rely on memory ...

                  As a side question, are we talking about mixing fortran and C/C++ or are you using qmake as a make generator only?

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    Jakub Srna
                    wrote on 11 Apr 2019, 10:13 last edited by
                    #9

                    Thank you all for your replies. I am sorry @kshegunov but I did not get the side question. But the way I understood I say yes, mixing the code. C++ is used to create the interface and Fortran holds all the advanced mathematical calculations.
                    Also, I got a step further. Was stupid and used MSVC and the gfortran, which is included just in the MinGW compiler. Got part of it compiled but at the same time getting plenty of errors. Guess that this is caused by the set flags.
                    To make the picture complete I developed the app in the Visual Studio before and I have the intel fortran in my PC. Can I include that somehow as I know it optimizes the code more?

                    And hopefully one of my last questions. When I use either gfortran or ifort in my code and I'll try to do the cross compilation. Will the fortran part of the code run flawlessly on Mac or Linux?

                    Thank you a lot

                    K 1 Reply Last reply 11 Apr 2019, 10:30
                    0
                    • J Jakub Srna
                      11 Apr 2019, 10:13

                      Thank you all for your replies. I am sorry @kshegunov but I did not get the side question. But the way I understood I say yes, mixing the code. C++ is used to create the interface and Fortran holds all the advanced mathematical calculations.
                      Also, I got a step further. Was stupid and used MSVC and the gfortran, which is included just in the MinGW compiler. Got part of it compiled but at the same time getting plenty of errors. Guess that this is caused by the set flags.
                      To make the picture complete I developed the app in the Visual Studio before and I have the intel fortran in my PC. Can I include that somehow as I know it optimizes the code more?

                      And hopefully one of my last questions. When I use either gfortran or ifort in my code and I'll try to do the cross compilation. Will the fortran part of the code run flawlessly on Mac or Linux?

                      Thank you a lot

                      K Offline
                      K Offline
                      kshegunov
                      Moderators
                      wrote on 11 Apr 2019, 10:30 last edited by kshegunov 4 Nov 2019, 10:30
                      #10

                      Guess that this is caused by the set flags.

                      I don't know.

                      Can I include that somehow as I know it optimizes the code more?

                      Fortran uses C-linkage so any linker should be able to manage it.
                      Note that fortran mandates decoration of the function names itself, because reasons. So if you have:

                      subroutine dummy()
                      end subroutine dummy
                      

                      This is seen by the linker as if it was defined as void dummy_().
                      If you intend to call fortran from C, you must provide the proper declarations for the compiler in your C code, they can't be deduced automatically.

                      When I use either gfortran or ifort in my code and I'll try to do the cross compilation. Will the fortran part of the code run flawlessly on Mac or Linux?

                      As long as you configure the compiler properly, you can treat fortran code as any other C code, so it should work fine, yes.

                      Tangent:
                      gfortran, I believe, still transpiles the fortran source to C and then runs the C compiler on top of it. In the end why should anyone invest time into keeping a real compiler for a zombie language up to date ...

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      2

                      2/10

                      10 Apr 2019, 06:28

                      topic:navigator.unread, 8
                      • Login

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