Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to use a static library?
Forum Updated to NodeBB v4.3 + New Features

how to use a static library?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 549 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.
  • L Offline
    L Offline
    lazuli_beast
    wrote on last edited by
    #1

    In MS Visual Studio, I created a fortran library 'Lib1' containing 'Source1.f90':

    subroutine fortfunc(ff)
    real*4 ff
    
    write(6,100) ff
    100 format('ff=',f6.3)
    
    return
    end
    

    I compiled it (x86) and it created a Lib1.lib file.

    In Qt Creator I opened the openglwindow example and added to main.cpp

    #include <iostream>
    using namespace std;
    extern "C" {
        void fortfunc_(float *ff);
    }
    

    and inside the main function

        float ff = 6.6;
        fortfunc_(&ff);
    

    In the .pro file I right clicked and added Lib1 as a static library.

    When I try to build the linker has an error:

    main.obj : error LNK2019: unresolved external symbol _fortfunc_ referenced in function _main
    

    Where did I go wrong? Thanks

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You also have to link against this library -> for qmake see LIBS

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      L 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        You also have to link against this library -> for qmake see LIBS

        L Offline
        L Offline
        lazuli_beast
        wrote on last edited by
        #3

        @Christian-Ehrlicher Ok, I figured it would take care of that for me. I added this line to my pro file and I get the same error

        win32:LIBS += C:\Users\[myname]\source\repos\Lib1\Lib1\Debug\Lib1.lib
        
        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Please try with forward slashes as shown in the documentation. Are you sure this libs exports the symbol in this way? Your function is named fortfunc in fortran but fortfunc_ in your c++ source.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          L 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            Please try with forward slashes as shown in the documentation. Are you sure this libs exports the symbol in this way? Your function is named fortfunc in fortran but fortfunc_ in your c++ source.

            L Offline
            L Offline
            lazuli_beast
            wrote on last edited by
            #5

            @Christian-Ehrlicher Ah, I didn't notice the slashes - that's fixed. I tried changing the function name to fortfunc in the c++ but it still has a problem with unresolved symbol.
            How can I tell what the actual name is in the exported library? Thanks

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @lazuli_beast said in how to use a static library?:

              How can I tell what the actual name is in the exported library? Thanks

              Don't know - ask fortran guys how to export/import a fortran function.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lazuli_beast
                wrote on last edited by
                #7

                Thanks so much for your help getting this far. After some googling I found an example I was able to follow to finally get it working.
                First, I changed my fortran to use the iso_c_binding. This lets you set the function name that you will call in the c++ program.

                subroutine fortfunc(ff) bind(c, name="fortfunc_bind")
                    use, intrinsic :: iso_c_binding
                    implicit none
                    
                    real*4 ff
                
                    write(6,100) ff
                    100 format('ff=',f6.3)
                
                    return
                end subroutine
                

                I got a different error, you have to also add the fortran library to the Qt project. In my case:

                win32:LIBS += -L"C:/Program Files (x86)/IntelSWTools/compilers_and_libraries_2020.4.311/windows/compiler/lib/ia32_win"
                
                1 Reply Last reply
                1

                • Login

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