MinGWとCmakeを使ってlapackをソースからビルドする時に見た地獄　 Hell to build lapack using MinGW and CMake

# 生じた問題　Occurred Problem

下記のサイトに従ってlapack3.8.0をビルドを試みた。
　　<font color="gray">I tried to build lapack 3.8.0 according to the instructions of the site below.</font>
[LAPACK for Windows "Build Instructions to create LAPACK and LAPACKE 3.5.0 dlls for Windows with MinGW"](https://icl.cs.utk.edu/lapack-for-windows/lapack/)

サイトに記載の8つ目の指示でlapack.dllが完成されるはずだった。
　　<font color="gray">The lapack.dll was supposed to be completed with the 8th instruction described on the site.</font>

```cmd.exe
buildfolder>C:/MinGW/bin/mingw32-make.exe
```

しかし次のようなエラーが生じた。
　　<font color="gray">However, the following error occurred.</font>

```cmd.exe
[  7%] Built target blas
[  8%] Linking Fortran shared library ..\bin\liblapack.dll
C:/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/libgfortran.a(string_intrinsics.o):(.text$_gfortran_concat_string+0x0): multiple definition of `_gfortran_concat_string'
../lib/libblas.dll.a(d000008.o):(.text+0x0): first defined here
C:/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/libgfortran.a(string_intrinsics.o):(.text$_gfortran_string_len_trim+0x0): multiple definition of `_gfortran_string_len_trim'
../lib/libblas.dll.a(d000038.o):(.text+0x0): first defined here
C:/MinGW/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'
../lib/libblas.dll.a(d000058.o):(.text+0x0): first defined here
C:/MinGW/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'
../lib/libblas.dll.a(d000053.o):(.text+0x0): first defined here
C:/MinGW/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'
../lib/libblas.dll.a(d000032.o):(.text+0x0): first defined here
C:/MinGW/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'
../lib/libblas.dll.a(d000033.o):(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
mingw32-make[2]: *** [SRC\CMakeFiles\lapack.dir\build.make:27686: bin/liblapack.dll] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:163: SRC/CMakeFiles/lapack.dir/all] Error 2
mingw32-make: *** [Makefile:162: all] Error 2
```

上記のエラーはシンボルの多重定義に起因するものと思われる。
　　<font color="gray">The above errors seem to be caused by overloading of symbols.</font>

# 解決方法　Solution
Cmakeが作成した次のファイルを編集することで解決する。
　　<font color="gray">It solves by editing the next file created by Cmake.</font>

**SRC\CMakeFiles\lapack.dir\link.txt**


次の一文をx86_64-w64-mingw32-gfortran.exe のオプションとして加える。
　　<font color="gray">Add the following sentence as an option of x86_64-w64-mingw32-gfortran.exe</font>

``` 
-Wl,--allow-multiple-definition
```

link.txtの該当行は次のようになる。
　　<font color="gray">The corresponding line of link.txt is edited as follows.</font>

```SRC\CMakeFiles\lapack.dir\link.txt
... 
C:\MinGW\bin\x86_64-w64-mingw32-gfortran.exe  -O2 -DNDEBUG -O2  -shared -o ..\bin\liblapack.dll -Wl,--allow-multiple-definition -Wl,--out-implib,..\lib\liblapack.dll.a -Wl,--major-image-version,3,--minor-image-version,8 -Wl,--whole-archive CMakeFiles\lapack.dir/objects.a -Wl,--no-whole-archive @CMakeFiles\lapack.dir\linklibs.rsp -Wl,--output-def,lapack.def
... 
```

これでC:/MinGW/bin/mingw32-make.exeを実行すればlapack.dllが作成されるはず。
　　<font color="gray">Now if you run C: /MinGW/bin/mingw32-make.exe, lapack.dll should be created.</font>

がんばろう。
 　　<font color="gray">Good Luck.</font>