【问题标题】:gfortran: compile source codes from hierarchically dependent subroutine filesgfortran:从分层依赖的子程序文件编译源代码
【发布时间】:2013-11-03 05:27:06
【问题描述】:

我要编译main.f90,它依赖于单独文件中的三个子例程。对于第三个子程序,它也有三个 sub_subroutines。

  1. sub1.f
  2. sub2.f
  3. sub3_main.f sub3_sub1.f sub3_sub2.f sub3_sub3.f

运行以下代码会产生如下长错误消息。

gfortran main.f90 sub1.f sub2.f sub3_main.f sub3_sub1.f sub3_sub2.f sub3_sub3.f -o- test.exe

我搜索了一下,发现可能需要flag-c,但是我不确定编译的顺序,以及如何将目标文件链接到一个独立的程序中。或者和这个问题有关:how to compile multi-folder Fortran Project having interfaces, modules and subroutines.

提前致谢!

test.exe: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crti.o:(.fini+0x0): first defined here
test.exe: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:(.data+0x0): first defined here
test.exe: In function `__data_start':
(.data+0x4): multiple definition of `__dso_handle'
/usr/lib/gcc/i686-linux-gnu/4.6/crtbegin.o:(.data+0x0): first defined here
test.exe:(.rodata+0x4): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here
test.exe: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:(.text+0x0): first defined here
test.exe:(.rodata+0x0): multiple definition of `_fp_hw'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:(.rodata+0x0): first defined here
test.exe: In function `main':
(.text+0x399): multiple definition of `main'
/tmp/ccwQ3UVQ.o:main.f90:(.text+0x20ee): first defined here
test.exe: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/i686-linux-gnu/4.6/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
test.exe:(.dtors+0x4): first defined here
/usr/bin/ld: error in test.exe(.eh_frame); no .eh_frame_hdr table will be created.
collect2: ld returned 1 exit status

【问题讨论】:

    标签: compiler-errors fortran gfortran


    【解决方案1】:

    您应该能够做到:

    gfortran -c sub1.f
    gfortran -c sub2.f
    gfortran -c sub3_main.f sub3_sub1.f sub3_sub2.f sub3_sub3.f
    gfortran sub1.o sub2.o sub3_main.o sub3_sub1.o sub3_sub2.o sub3_sub3.o  main.f90 -o text.exe
    

    gfortran sub1.f sub2.f sub3_main.f sub3_sub1.f sub3_sub2.f sub3_sub3.f main.f90 -o text.exe
    

    您应该只有一个程序和任意数量的程序(子程序和函数)。以上假设 sub3_main 是一个程序,并且您的程序在 main.f90 中。

    【讨论】:

    • 谢谢@M。 SB!你是对的,sub3_main.f 是一个依赖于sub3_sub1.f sub3_sub2.f sub3_sub3.f 的过程。但我不知道为什么这两种方法都会产生与我之前遇到的相同的错误。
    • 如果您仍然遇到相同的错误,我想您需要向我们展示一些代码。您真的在所有文件中都有一个程序吗?您是否在某处使用名为 main 的变量?
    • 感谢@haraldkl!我在main.f90 中只有一个程序,并且在所有文件中都没有名为main 的变量。我用下面的代码测试了每组子程序,它们都可以正常工作。 gfortran test_sub1.f90 sub1.f -o- test_sub1.exegfortran test_sub2.f90 sub2.f -o- test_sub2.exegfortran test_sub3.f90 sub3_main.f sub3_sub1.f sub3_sub2.f sub3_sub3.f -o- test_sub3.exe。对不起,源代码一团糟,我只能提供抽象文件名。
    • 您在多个文件中是否有program 声明?否则你怎么能从文件的各种子集制作可执行文件?应该只有一个主程序。
    • 对于这个问题,我在main.f90 中只有一个program 语句。为了测试子程序,正如我在上面的评论中提到的,我之前在test_sub1.f90 test_sub2.f90 test_sub3.f90 中使用了program 语句。
    猜你喜欢
    • 1970-01-01
    • 2019-06-01
    • 2014-09-07
    • 2011-04-14
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    • 2013-01-23
    • 1970-01-01
    相关资源
    最近更新 更多