use a function written by Fortran source code in Qt,the result of function is right by 32bit compiler,the result is wrong by 64bit compiler



  • Hello Everyone:
    I have recently met a strange problem about Qt&Fortran integrating program.In my Desktop App,the main function frame is written by Qt, I need a calculated function whose source code was written by Fortran,but not a .dll lib file.And I confirm that all the source code (Qt&Fortran) are right,The program could be compiled normally and run in my computer.I use 2 kind of compilers.The result of function which was compiled by 32-bit GNU-Mingw is right, I can get what I expected. However, when the code is compiled by 64-bit GNU-Mingw, Although the program can run , the result of calculated function is wrong. I check out the array matrix by qDebug, every value in my array is "NAN".I have doubt about the pointer is beyond the boundary of the array when the matrix is transferring from Qt to Fortran.My operating system is X86_64 Windows7.I really really what to know what is the reason for that problem and how to solve this fatal problem.

    Looking forward to your reply & Thanks again for your patience
    tudiudiu


  • Moderators

    @tudiudiu

    How were the Qt libraries compiled when you combine them with the 64bit compiled FORTRAN?

    IIRC the question might if the memory model is the same for 64bit FORTRAN and C++ compiled with the same MinGW suite.

    IMHO it is at the day's end not an issue of Qt, but more something with the compilers and the memory handling there.


  • Qt Champions 2017

    @tudiudiu, as @koahnig already said this is more likely a compiler problem. As you already noted, it may be pointer related. 64 bit C++ compiler operate with 64 bit pointers, while 32 bit compilers use 32 bit pointers.

    so the question: how do you combine your C++ and Fortran code? and which compiler do you use to compile fortran? do you compile Fortran to 64 bit also?



  • @tudiudiu said in use a function written by Fortran source code in Qt,the result of function is right by 32bit compiler,the result is wrong by 64bit compiler:

    et a strange problem about Qt&Fortran integrating program.In my Desktop App,the main function frame is written by Qt, I need a calculated function whose source code was written by Fortran,but not a .dll lib file.And I confirm that all the source code (Qt&Fortran) are right,The program could be compiled normally and run in my computer.I use 2 kind of compilers.The result of function which was compiled by 32-bit GNU-Mingw is right, I can get what I expected. However, when the code is compiled by 64-bit GNU-Mingw, Although the program can run , the result of calculated function is wrong. I check out the array matrix by qDebug, every

    Hi~~~@koahnig @aha_1980
    In my Qt .pro file,the combine style is remarked as “lib += lgfortran,source += /abc.f90”.
    In my Fortran file, the function is named as "sub(arg1,arg2,arg3......argN)"
    In my main function file,Remark : extern "C" {void sub_(int *m, int *n, float x[], float fvec[], int *iflag);}
    Then the Fortran's Subroutine is used by Qt as an external void function so that the compiler could compile the Qt & fortran at the same time. In my Qt Creator Edit , I have configured 2 kind of compile chain kit, MingW32 & MingW64. Even if the source code is same, but I can choose one compiler i need at one time(MingW32 or MingW64).

    As you already noted, the problem has something to do with the memory handling and pointer length.
    The argument function in Fortran expect a 32-bit pointer transferred by Qt , but for 64bit compiler, the length of pointer is 64-bit. So I am planning to rewrite the Fortran code , for example "Integer *4 -> Integer 8, Real4 ->Double *8". I'm not sure whether it would work or not. I will reply to you @koahnig @aha_1980 as soon as i get the result.

    Really Appreciate for your guide And thanks again ~~~

    tudiudiu


  • Qt Champions 2017

    Hi @tudiudiu

    I'm sorry, but I have absolutely no Fortran knowledge.

    So if I understand you correctly, you have an entry SOURCES += abc.f90 in your .profile. That means, abc.f90 is your Fortran source file, correct?

    That means, qmake generates a Makefile which calls the Fortran compiler, correct too?

    So have you checked that the correct Fortran compiler (64 bit) is called for your 64 bit code?

    Regards


  • Moderators

    @aha_1980

    IMHO the lack of Fortran knowledge is actually an advantage. I started out with Fortran very long ago and have pushed all knowledge to the back where the cobwebs reside ;)

    More seriously. As @aha_1980 already mentions did you check that you are using 64 bit compilers in both cases?
    Basically I would suggest that you are using the same MinGW compiler for both Fortran and C++.

    You did not give information of the library you are using from Fortran. I am typically going the route to convert Fortran to C/C++ and compile. However, that might be only an option with smaller libraries, since I was substituting the few Fortran emulating library calls with C/C++ calls when required.



  • @aha_1980 @koahnig
    Hi~my friends,Thanks for your kindly help, And i have been trying to find this bug as the advice you give.

    First of all, I check the compilers to make sure there is only an effective tool chain. All of the source code were transferred to another machine(only with MingW64) to compile , The Result is still "NAN".However ,When the source code were transferred to a machine(only with MingW32) to compile, the Result is acceptable.

    Secondly,I attempted to check whether there are a few lib(32bit dynamic or static) in Fortran code. At last, The result is that there is not other lib at all, The target function is written by pure Fortran format.

    Thirdly, I rewrite part of the Fortran code with regarding to the variable declaration so that the length of variable could match the length of pointer in 64-bit system . For example "Integer *4 -> Integer 8, Real4 ->Double *8". But it doesn't work.

    Finally, I change another new simple function ,Transfer a 2-Dimension matrix from Fortran to Qt, The result is totally correct.

    So the bug seem to be nothing to do with the compilers .
    I think the Fortran code i got from my company is wrong. I can't not imagine my colleague take that kind of stupid mistake , What makes me unexpected more is my boss have checked and give him "PASS".=_=|||

    I feel so apologetic for my fault.I should have think about this problem.
    Sincerely gratitude for your help again.@aha_1980 @koahnig

    Regards
    tudiudiu


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.