0
我想编译main.f90
,它依赖于三个子程序在单独的文件中。对于第三个子程序,它也有三个子程序。gfortran:编译分层相关子程序文件的源代码
sub1.f
sub2.f
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
我搜索,发现我可能需要标志-c
,但我不知道有关编译的命令,再怎么目标文件链接成一个独立的程序。或者它与这个问题有关:。
在此先感谢!
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
谢谢@M。 S. B.!你说得对,'sub3_main.f'是一个依赖于'sub3_sub1.f sub3_sub2.f sub3_sub3.f'的过程。但我不知道为什么两种方法都会产生与我以前遇到的相同的错误。 – Randel
如果你仍然得到相同的错误,我想你需要向我们展示一些代码。你真的在所有文件中都有一个程序吗?你使用称为main的变量吗? – haraldkl
Thanls @haraldkl!我在'main.f90'中只有一个程序,并且在所有文件中都没有变量'main'。我用以下代码测试了每组子程序,并且它们可以很好地工作。 'gfortran test_sub1.f90 sub1.f -o- test_sub1.exe','gfortran test_sub2.f90 sub2.f -o- test_sub2.exe','gfortran test_sub3.f90 sub3_main.f sub3_sub1.f sub3_sub2.f sub3_sub3.f - o-test_sub3.exe'。抱歉源代码乱七八糟,我只能提供抽象文件名。 – Randel