how to use a static library?
-
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
-
You also have to link against this library -> for qmake see LIBS
-
You also have to link against this library -> for qmake see LIBS
@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
-
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 butfortfunc_
in your c++ source. -
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 butfortfunc_
in your c++ source.@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 -
@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.
-
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"