【发布时间】: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)
【问题讨论】:
-
看起来您需要链接一些 Fortran 库以获取
fortfunc()正在执行 I/O 的调用。不幸的是,我不知道该怎么做。 -
其他链接可以通过最少的谷歌搜索轻松获得。
-
我在问题的 cmake 文件中找不到您的评论中的行。这将有助于明确查看 cmake 文件中包含 libgfortran 的位置。
标签: c++ visual-c++ cmake fortran gfortran