0

My gfortran version is 8.1.0,and I use CMake to build a Fortran project.
When I keep all the source code in single folder and compile them into exe, it works. But if I divide them into two pieces, one for compiling dll and another for compiling exe, then I link this exe to the dll, it will have problem, the error message is following:

[build] Starting build  
[proc] Executing command: E:\cmake-3.26.3-windows-x86_64\cmake-3.26.3-windows-x86_64\bin\cmake.EXE --build d:/HEU_C_Fortran/example/build --config Debug --target test_bellhop -j 14 --  
[build] [  4%] Building Fortran object fortran_prj/Bellhop/CMakeFiles/bellhopLib.dir/RefCoMod.f90.obj  
[build] [  9%] Building Fortran object fortran_prj/Bellhop/CMakeFiles/bellhopLib.dir/SdRdRMod.f90.obj  
[build] [ 14%] Building Fortran object fortran_prj/Bellhop/CMakeFiles/bellhopLib.dir/angleMod.f90.obj  
[build] [ 19%] Building Fortran object fortran_prj/Bellhop/CMakeFiles/bellhopLib.dir/bdryMod.f90.obj  
[build] [ 23%] Building Fortran object fortran_prj/Bellhop/CMakeFiles/bellhopLib.dir/ArrMod.f90.obj  
[build] [ 28%] Building Fortran object fortran_prj/Bellhop/CMakeFiles/bellhopLib.dir/beampatternMod.f90.obj  
[build] [ 33%] Building Fortran object fortran_prj/Bellhop/CMakeFiles/bellhopLib.dir/sspMod.f90.obj  
[build] [ 38%] Building Fortran object fortran_prj/Bellhop/CMakeFiles/bellhopLib.dir/WriteHeader.f90.obj  
[build] [ 42%] Building Fortran object fortran_prj/Bellhop/CMakeFiles/bellhopLib.dir/bellMod.f90.obj  
[build] [ 47%] Building Fortran object fortran_prj/Bellhop/CMakeFiles/bellhopLib.dir/cpu_time.f90.obj  
[build] [ 52%] Building Fortran object fortran_prj/Bellhop/CMakeFiles/bellhopLib.dir/munk.f90.obj  
[build] [ 57%] Building Fortran object fortran_prj/Bellhop/CMakeFiles/bellhopLib.dir/errout.f90.obj  
[build] [ 61%] Building Fortran object fortran_prj/Bellhop/CMakeFiles/bellhopLib.dir/polyz.f90.obj  
[build] [ 66%] Building Fortran object fortran_prj/Bellhop/CMakeFiles/bellhopLib.dir/spliner.f90.obj  
[build] [ 71%] Building Fortran object fortran_prj/Bellhop/CMakeFiles/bellhopLib.dir/subtab.f90.obj  
[build] [ 76%] Building Fortran object fortran_prj/Bellhop/CMakeFiles/bellhopLib.dir/sorti.f90.obj  
[build] [ 80%] Building Fortran object fortran_prj/Bellhop/CMakeFiles/bellhopLib.dir/weight.f90.obj  
[build] [ 85%] Building Fortran object fortran_prj/Bellhop/CMakeFiles/bellhopLib.dir/readin.f90.obj  
[build] [ 90%] Linking Fortran shared library ..\..\out\libbellhopLib.dll  
[build] E:\cmake-3.26.3-windows-x86_64\cmake-3.26.3-windows-x86_64\bin\cmake.exe -E rm -f CMakeFiles\bellhopLib.dir/objects.a  
[build] E:\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\mingw64\bin\ar.exe qc CMakeFiles\bellhopLib.dir/objects.a @CMakeFiles\bellhopLib.dir\objects1.rsp  
[build] E:\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\mingw64\bin\gfortran.exe -g -shared -o ..\..\out\libbellhopLib.dll -Wl,--out-implib,..\..\out\libbellhopLib.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -Wl,--whole-archive CMakeFiles\bellhopLib.dir/objects.a -Wl,--no-whole-archive   
[build] [ 90%] Built target bellhopLib  
[build] [ 95%] Building Fortran object fortran_prj/Bellhop/test/CMakeFiles/test_bellhop.dir/bellhop.f90.obj  
[build] [100%] Linking Fortran executable ..\..\..\out\test_bellhop.exe  
[build] E:\cmake-3.26.3-windows-x86_64\cmake-3.26.3-windows-x86_64\bin\cmake.exe -E rm -f CMakeFiles\test_bellhop.dir/objects.a  
[build] E:\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\mingw64\bin\ar.exe qc CMakeFiles\test_bellhop.dir/objects.a @CMakeFiles\test_bellhop.dir\objects1.rsp  
[build] E:\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\mingw64\bin\gfortran.exe -g -Wl,--whole-archive CMakeFiles\test_bellhop.dir/objects.a -Wl,--no-whole-archive -o ..\..\..\out\test_bellhop.exe -Wl,--out-implib,libtest_bellhop.dll.a -Wl,--major-image-version,0,--minor-image-version,0 @CMakeFiles\test_bellhop.dir\linkLibs.rsp
[build] E:/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/libgfortran.a(string_intrinsics.o):(.text$_gfortran_string_scan+0x0): multiple definition of `_gfortran_string_scan'  
[build] ../../../out/libbellhopLib.dll.a(d000132.o):(.text+0x0): first defined here  
[build] E:/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/libgfortran.a(compile_options.o):(.text$_gfortran_set_options+0x0): multiple definition of `_gfortran_set_options'
[build] ../../../out/libbellhopLib.dll.a(d000107.o):(.text+0x0): first defined here  
[build] E:/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/libgfortran.a(main.o):(.text$_gfortran_set_args+0x0): multiple definition of `_gfortran_set_args'
[build] ../../../out/libbellhopLib.dll.a(d000103.o):(.text+0x0): first defined here  
[build] E:/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/libgfortran.a(transfer.o):(.text$_gfortran_transfer_integer_write+0x0): multiple definition of `_gfortran_transfer_integer_write'
[build] ../../../out/libbellhopLib.dll.a(d000150.o):(.text+0x0): first defined here  
[build] E:/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/libgfortran.a(transfer.o):(.text$_gfortran_transfer_real_write+0x0): multiple definition of `_gfortran_transfer_real_write'
[build] ../../../out/libbellhopLib.dll.a(d000154.o):(.text+0x0): first defined here  
[build] E:/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/libgfortran.a(transfer.o):(.text$_gfortran_transfer_character_write+0x0): multiple definition of `_gfortran_transfer_character_write'
[build] ../../../out/libbellhopLib.dll.a(d000145.o):(.text+0x0): first defined here  
[build] E:/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/libgfortran.a(transfer.o):(.text$_gfortran_transfer_complex_write+0x0): multiple definition of `_gfortran_transfer_complex_write'
[build] ../../../out/libbellhopLib.dll.a(d000147.o):(.text+0x0): first defined here  
[build] E:/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/libgfortran.a(transfer.o):(.text$_gfortran_st_write+0x0): multiple definition of `_gfortran_st_write'
[build] ../../../out/libbellhopLib.dll.a(d000122.o):(.text+0x0): first defined here  
[build] E:/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/libgfortran.a(transfer.o):(.text$_gfortran_st_write_done+0x0): multiple definition of `_gfortran_st_write_done'
[build] ../../../out/libbellhopLib.dll.a(d000123.o):(.text+0x0): first defined here  
[build] collect2.exe: error: ld returned 1 exit status  
[build] mingw32-make.exe[3]: *** [fortran_prj\Bellhop\test\CMakeFiles\test_bellhop.dir\build.make:101: out/test_bellhop.exe] Error 1  
[build] mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:325: fortran_prj/Bellhop/test/CMakeFiles/test_bellhop.dir/all] Error 2  
[build] mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:332: fortran_prj/Bellhop/test/CMakeFiles/test_bellhop.dir/rule] Error 2  
[build] mingw32-make.exe: *** [Makefile:201: test_bellhop] Error 2  
[proc] The command: E:\cmake-3.26.3-windows-x86_64\cmake-3.26.3-windows-x86_64\bin\cmake.EXE --build d:/HEU_C_Fortran/example/build --config Debug --target test_bellhop -j 14 -- exited with code: 2  
[build] Build finished with exit code 2

I am using source code like this:

PROGRAM BELLHOP

  ! Beam tracing in cylindrical coordinates
  ! Michael B. Porter and Homer P. Bucker

  USE bellMod
  USE RefCoMod
  USE bdryMod
  USE angleMod
  USE SdRdRMod
  USE ArrMod
  USE BeamPatternMod

  ! note ArrivalsStorage of 2000000 is about the max g95 could take in the allocate statement
  INTEGER, PARAMETER    :: SHDFIL = 25, RAYFIL = 21, ArrivalsStorage = 20000000
  REAL,    PARAMETER    :: DegRad = pi / 180.0
  INTEGER   IBPvec( 1 )
  REAL      xs( 2 ), gradc( 2 )
  COMPLEX,  ALLOCATABLE ::   U( :, : )
  COMPLEX   EPS, PICKEPS
  CHARACTER TITLE*80, BotOpt*3, RunType*4, BeamType*3

  CALL CPU_TIME( Tstart )

  ! Read in control data ***

  CALL READIN( TITLE, freq, ISINGL, &
       NIMAGE, IBWIN, deltas, MaxN, zBox, rBox, EPMULT, RLOOP,  &
       TopOpt, DepthT, CPT, RHOT, BotOpt, DepthB, CPB, RHOB, RunType, BeamType )

  CALL READATI(  TopOpt(5:5), DepthT, rBox, PRTFil )    ! READ AlTImetry
  CALL READBTY(  BotOpt(2:2), DepthB, rBox, PRTFil )      ! READ BaThYmetrY
  CALL READRC(   BotOpt(1:1), TopOpt(2:2),  PRTFil )    ! READ Reflection Coefficients (top and bottom)
  CALL READPAT( RunType(3:3),               PRTFil )      ! Read Source Beam Pattern
...
...
...
END PROGRAM BELLHOP

SUBROUTINE func_name(arglist)
    ...
END SUBROUTINE func_name
...
...

dll source code like this:


SUBROUTINE SHIFTIN( X, T, I1, I2 )

  ! Shift values (I1, I2) one position to the right
  !  and insert T at position I1

SUBROUTINE SORT( X, N )

  ! Does an insertion sort on a vector of real numbers

  ! At the Ith step, the first I-1 positions contain a sorted
  ! vector.  We shall insert the Ith value into its place in that
  ! vector shifting up to produce a new vector of length I.

  REAL X( * )

  IF ( N == 1 ) RETURN

  DO I = 2, N

     T = X( I )

     IF ( T < X( 1 ) ) THEN
        ! Goes in the first position
        CALL SHIFTIN( X, T, 1, I - 1 )

     ELSE IF ( T < X( I - 1 ) ) THEN

        ! *** Binary search for its place ***

        IRIGHT = I - 1
        ILEFT  = 1

        DO WHILE ( IRIGHT > ILEFT + 1 )
           IMID = ( ILEFT + IRIGHT ) / 2
           IF ( T < X(IMID) ) THEN
              IRIGHT = IMID
           ELSE
              ILEFT  = IMID
           ENDIF
        END DO

        ! Shift and insert
        CALL SHIFTIN( X, T, IRIGHT, I - 1 )

     ENDIF

  END DO

  RETURN
END SUBROUTINE SORT
  REAL X( * )

  DO  K = I2, I1, -1
     X( K + 1 ) = X( K )
  END DO

  X( I1 ) = T

  RETURN
END SUBROUTINE SHIFTIN
stone
  • 1
  • 1
  • Welcome, I suggest taking the [tour]. Please never show error messages as a picture. Always copy and paste them **as text**. It is really important. Also, we need the command that you used to compile the code. It also looks like you used some build system, such as CMake or similar. In that case the configuration of this system will be necessary. See also [ask]. If it is indeed CMake, please use `make VERBOSE=1`. See https://stackoverflow.com/questions/2670121/using-cmake-with-gnu-make-how-can-i-see-the-exact-commands for more. – Vladimir F Героям слава Apr 20 '23 at 08:53
  • Ah, bellhop. I rewrote the Makefiles for bellhop years ago for FreeBSD's bmake. Fun times. Your errors looks like you're adding more than one main program to a library and then trying to link the library with some program. – steve Apr 20 '23 at 18:07
  • @steve Thank you for your suggestion, but there is only one main program in my source code, and I found a way to solve the problem yesterday according to: https://qiita.com/kimurap/items/756e7d38622cad34773b – stone Apr 21 '23 at 10:21
  • @VladimirFГероямслава Thanks for your suggestions, I paste the error messages as text and use `make VERBOSE=1` now. – stone Apr 21 '23 at 10:28
  • Well, that's an ugly solution and should be unneeded. MingW or the cmake must be doing something quite strange with linking the runtime library into each and everything object file. – steve Apr 21 '23 at 14:49
  • @steve yep, I think so too.In my opinion, maybe the gfortran in MingW links the same option symbols to all targets which it builds, results of multiple definitions – stone Apr 21 '23 at 15:57
  • If you're stuck in a MS Windows environment, you ay want to consider loading WSL2 with ubuntu 22.xxx and then using gfortran under WSL2. Works for me – steve Apr 21 '23 at 16:17

0 Answers0