1

I compile Fortran small programm by GFortran and try to call subroutine from C++ code compilled by MSVC 2013 (like this). I get next errors on linkage:

  • error LNK2019: unresolved external symbol _gfortran_st_write referenced in function fortfunc_
  • error LNK2019: unresolved external symbol _gfortran_transfer_integer_write referenced in function fortfunc_
  • error LNK2019: unresolved external symbol _gfortran_transfer_real_write referenced in function fortfunc_
  • error LNK2019: unresolved external symbol _gfortran_st_write_done referenced in function fortfunc_

I build Fortran code to static lib (ar rc .lib .o), and link it to C++ project.

Can you show me mistake, how I call gfortran subrutine grom msvc c++ code?

My C++ code:

#include <iostream>
using namespace std;

extern "C" {
    void fortfunc_(int *ii, float *ff);
}

int main( int argc, char **argv )
{
    int ii=5;
    float ff=5.5;

    fortfunc_(&ii, &ff);
}

And Fortran code:

  subroutine fortfunc(ii,ff)
       integer ii
       real*4  ff

       write(6,100) ii, ff
  100  format('ii=',i2,' ff=',f6.3)

       return
       end

Cmake:

add_executable(${UNIT} ${_UNIT_SOURCES})
set(FORTRAN_TEST_LIB testlib.lib) 
target_link_libraries(${UNIT} ${GTEST_LIBRARY} ${GTEST_MAIN_LIBRARY} 
     ${FORTRAN_TEST_LIB})

If I link with libgfortran and other mingw libs I get:

LNK1000 - error during BuildImage

LINK : fatal error LNK1000: Internal error during IMAGE::BuildImage

Version 12.00.40629.0

ExceptionCode            = C0000005
ExceptionFlags           = 00000000
ExceptionAddress         = 00F8980B (00F70000) 
                 "C:\PROGRA~2\MICROS~3.0\VC\bin\X86_AM~1\link.exe"
NumberParameters         = 00000002
ExceptionInformation[ 0] = 00000000
ExceptionInformation[ 1] = 00000010

CONTEXT:
Eax    = 00000000  Esp    = 00E3E1D4
Ebx    = 80400000  Ebp    = 00E3E1F4
Ecx    = C0800000  Esi    = 80652AFC
Edx    = 80652B20  Edi    = 8065291C
Eip    = 00F8980B  EFlags = 00010202
SegCs  = 00000023  SegDs  = 0000002B
SegSs  = 0000002B  SegEs  = 0000002B
SegFs  = 00000053  SegGs  = 0000002B
Dr0    = 00000000  Dr3    = 00000000
Dr1    = 00000000  Dr6    = 00000000
Dr2    = 00000000  Dr7    = 00000000
LINK Pass 1 failed. with 1000

and my cmake file:

set(UNIT unit_tests)

include_directories(${CMAKE_CURRENT_SOURCE_DIR} src)

file(GLOB_RECURSE _UNIT_SOURCES "src/*.cpp")
file(GLOB_RECURSE _GNU 
"D:/Development/COMMON_UTILS/GNU/mingw64/lib/gcc/x86_64-w64-mingw32/6.3.0/*.a")

file(GLOB_RECURSE _TEST_RES "res/*.ini")
add_executable(${UNIT} ${_UNIT_SOURCES})

target_link_libraries(${UNIT} ${GTEST_LIBRARY} 
${GTEST_MAIN_LIBRARY} ${_GNU} ${FORTRAN_TEST_LIB})

add_test(test1 ${UNIT})
install (TARGETS ${UNIT} RUNTIME DESTINATION bin)
install(FILES ${_TEST_RES} DESTINATION test_res)
  • It looks like you need to link in some Fortran libraries for the calls that `fortfunc()` is making to perform the I/O. Unfortunately I have no idea how to do that. – Michael Burr Apr 20 '17 at 23:22
  • http://stackoverflow.com/questions/25384590/compiling-mixed-c-c-code-with-fortran-using-visual-studio-2013-and-intel-fortr – Michael Dorgan Apr 20 '17 at 23:23
  • https://msdn.microsoft.com/en-us/library/aa270933(v=VS.60).aspx – Michael Dorgan Apr 20 '17 at 23:23
  • And other links are easily available with minimal google searching. – Michael Dorgan Apr 20 '17 at 23:24
  • Possible duplicate of [Compiling mixed C++/C code with Fortran using Visual Studio 2013 and Intel Fortran](http://stackoverflow.com/questions/25384590/compiling-mixed-c-c-code-with-fortran-using-visual-studio-2013-and-intel-fortr) – Michael Dorgan Apr 20 '17 at 23:25
  • @FatherOctober: You should read through the gfortran [Mixed Language Programming](https://gcc.gnu.org/onlinedocs/gfortran/Mixed-Language-Programming.html) docs. And you probably also need to link with the `libgfortran` library. – Michael Burr Apr 20 '17 at 23:54
  • You might also consider getting things to work with g++ linking to gfortran first in case using Microsoft tools adds something else to the mix. – Michael Burr Apr 20 '17 at 23:56
  • Possible duplicate of [Linking fortran and c++ binaries using gcc](http://stackoverflow.com/questions/5663083/linking-fortran-and-c-binaries-using-gcc) – Vladimir F Героям слава Apr 21 '17 at 06:44
  • Also see http://stackoverflow.com/questions/19148378/gfortran-pow-c8-i4-error-when-linking-o-files-from-g-and-gfortran-using-g/19149757 http://stackoverflow.com/questions/10802778/linking-gfortran-libraries-to-c-prorgam-in-codeblocks-under-ubuntu – Vladimir F Героям слава Apr 21 '17 at 06:45
  • Also http://stackoverflow.com/questions/38989158/problems-calling-fortran-library-from-c this has ben treated many times before. – Vladimir F Героям слава Apr 21 '17 at 06:46
  • @VladimirF I agree with you - there are some similar questions. In my case the problem is that I have legacy code compiled by MSVC 2013-64 and GFortran by GNU/Mingw64. I try to link with libgfortran: `set(FORTRAN_TEST_LIB testlib.lib) set(FORTRAN_RUNTIME libgfortran.a) # or libgfortran.lib # target_link_libraries(${UNIT} ${GTEST_LIBRARY} ${GTEST_MAIN_LIBRARY} ${FORTRAN_RUNTIME} ${FORTRAN_TEST_LIB})` When I did it I get LNK1000 - error during BuildImage, because I try bind mingw library with msvc code. – FatherOctober Apr 21 '17 at 10:19
  • 1
    I can't find the lines from your comment in your cmake file in the question. I t would help to see explicitly where libgfortran is included in the cmake file. – Vladimir F Героям слава Apr 21 '17 at 14:54

1 Answers1

0

Using cmake_add_fortran_subdirectory helped to resolve problem. See https://blog.kitware.com/fortran-for-cc-developers-made-easier-with-cmake/ for example.