Fortran Coder

标题: 新人求教, fortran控制台引用dll, 编译不通过 [打印本页]

作者: huxiao    时间: 7 小时前
标题: 新人求教, fortran控制台引用dll, 编译不通过
本帖最后由 huxiao 于 2025-9-17 15:57 编辑

现有一个解决方案下有三个项目其中fortran相关有2个,console和 dynamic-link Library  ,  console控制台用于调试 fortran 编译的dll.  
C# winform 调用fortran编译的dll负责界面化.

dll项目属性设置如下
general/output directory =$(SolutionDir)lib
fortran/Output files/module path=$(SolutionDir)lib

console 项目属性设置如下
fortran/Additional Include Directories=$(solutionDir)lib
linker/Additional Library Directories=$(solutionDir)lib
linker/input/Additional Dependecies=MathFunctions.lib

MathFunctions.dll项目 代码如下
[Fortran] syntaxhighlighter_viewsource syntaxhighlighter_copycode
module mathfunctions_export
    use, intrinsic :: iso_c_binding, only: c_float
    implicit none

contains

    function add(a, b) result(res) bind(c, name="add")
        import :: c_float
        real(c_float), value :: a, b
        real(c_float) :: res
        res = a + b
    end function add

end module mathfunctions_export


console 项目代码如下
[Fortran] syntaxhighlighter_viewsource syntaxhighlighter_copycode
program FortranConsole
    use, intrinsic :: iso_c_binding, only: c_float  ! 如果需要在主程序里也用 c_float
    use mathfunctions_export           ! use 你要的模块和函数
    implicit none

    ! 本地变量
    real(c_float) :: num1, num2, result
    integer :: choice
    logical :: running =.true.

    ! 调用 add 函数
    num1 = 5.0_c_float  ! 注意:如果你用了 c_float,字面量要带 _c_float 后缀
    num2 = 3.0_c_float
    result = add(num1, num2)  ! 直接调用模块里的 add
    print *, "Result of addition: ", result

end program FortranConsole



已确定解决方案根目录下 lib文件夹内 已生成 MathFunctions.dll , mathfunctions_export.mod , MathFunctions.lib ,MathFunctions.pdb

现在问题 当编译 fortran coonsole 时, 报错 error LNK2019: 无法解析的外部符号 add,函数 MAIN__ 中引用了该符号 ,  始终找不到原因在哪里








欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2