【问题标题】:MSVC C++ calling GFortran subroutineMSVC C++ 调用 GFortran 子例程
【发布时间】:2017-04-20 23:16:05
【问题描述】:

我通过 GFortran 编译 Fortran 小程序,并尝试从 MSVC 2013 编译的 C++ 代码中调用子程序(如 this)。我在链接上遇到下一个错误:

  • 错误 LNK2019:未解析的外部符号 _gfortran_st_write 在函数 fortfunc_ 中引用
  • 错误 LNK2019:未解析的外部符号 _gfortran_transfer_integer_write 在函数 fortfunc_ 中引用
  • 错误 LNK2019:未解析的外部符号 _gfortran_transfer_real_write 在函数 fortfunc_ 中引用
  • 错误 LNK2019:未解析的外部符号 _gfortran_st_write_done 在函数 fortfunc_ 中引用

我将 Fortran 代码构建到静态库 (ar rc .lib .o),并将其链接到 C++ 项目。

你能告诉我错误吗,我如何调用 gfortran subrutine grom msvc c++ 代码?

我的 C++ 代码:

#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);
}

和 Fortran 代码:

  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})

如果我链接 libgfortran 和其他 mingw 库,我会得到:

LNK1000 - 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

还有我的 cmake 文件:

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)

【问题讨论】:

标签: c++ visual-c++ cmake fortran gfortran


【解决方案1】:

使用 cmake_add_fortran_subdirectory 有助于解决问题。例如,请参阅https://blog.kitware.com/fortran-for-cc-developers-made-easier-with-cmake/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-14
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    相关资源
    最近更新 更多