1
votes

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)
1
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
And other links are easily available with minimal google searching. - Michael Dorgan
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

1 Answers

0
votes

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