Spazz
Spazz

Reputation: 403

MSYS2 MinGW64 to build GMP/MPFR on Windows as static library, and link them in MSVC project compiled with CL

I'm working using Visual Studio 2019. I have a project that requires GMP and MPFR ; as far as I know, there are no recent binaries available for Windows and one has to build his own.

To build GMP/MPFR from source code, I used the mingw-w64-x86_64-toolchain installed with the command pacman -S mingw-w64-x86_64-toolchain in MSYS2 shell. Then, from the MinGW64 shell, I just used ./configure, make, make install and ended up with .a libraries, which is, unless I'm wrong, perfectly usable in a VS project.

So now back to my project in Visual Studio, I'm trying to compile it with default compiler CL => it fails with the following errors :

Severity    Code    Description                                                                             Project     File                                                Line    Suppression State
Error   LNK1120     2 unresolved externals                                                                  MyProject   MyProject.exe                                       1   
Error   LNK2001     unresolved external symbol ___chkstk_ms                                                 MyProject   libgmp.a(aors.o)                                    1   
Error   LNK2001     unresolved external symbol ___chkstk_ms                                                 MyProject   libgmp.a(dcpi1_bdiv_q.o)                            1   
Error   LNK2001     unresolved external symbol ___chkstk_ms                                                 MyProject   libgmp.a(dcpi1_bdiv_qr.o)                           1   
Error   LNK2019     unresolved external symbol ___chkstk_ms referenced in function mpn_dcpi1_divappr_q_n    MyProject   libgmp.a(dcpi1_divappr_q.o)                         1   
Error   LNK2001     unresolved external symbol ___chkstk_ms                                                 MyProject   libgmp.a(dcpi1_div_qr.o)                            1   
Error   LNK2001     unresolved external symbol ___chkstk_ms                                                 MyProject   libgmp.a(divexact.o)                                1   
Error   LNK2001     unresolved external symbol ___chkstk_ms                                                 MyProject   libgmp.a(gcd.o)                                     1   
Error   LNK2001     unresolved external symbol ___chkstk_ms                                                 MyProject   libgmp.a(hgcd_reduce.o)                             1   
Error   LNK2001     unresolved external symbol ___chkstk_ms                                                 MyProject   libgmp.a(invertappr.o)                              1   
Error   LNK2001     unresolved external symbol ___chkstk_ms                                                 MyProject   libgmp.a(lt103-divexact.o)                          1   
Error   LNK2001     unresolved external symbol ___chkstk_ms                                                 MyProject   libgmp.a(lt31-mul.o)                                1   
Error   LNK2001     unresolved external symbol ___chkstk_ms                                                 MyProject   libgmp.a(lt55-cmp.o)                                1   
Error   LNK2001     unresolved external symbol ___chkstk_ms                                                 MyProject   libgmp.a(lt57-cmp_ui.o)                             1   
Error   LNK2001     unresolved external symbol ___chkstk_ms                                                 MyProject   libgmp.a(lt64-mul.o)                                1   
Error   LNK2001     unresolved external symbol ___chkstk_ms                                                 MyProject   libgmp.a(lt82-mul.o)                                1   
Error   LNK2001     unresolved external symbol ___chkstk_ms                                                 MyProject   libgmp.a(lt97-gcd.o)                                1   
Error   LNK2001     unresolved external symbol ___chkstk_ms                                                 MyProject   libgmp.a(lt99-tdiv_qr.o)                            1   
Error   LNK2001     unresolved external symbol ___chkstk_ms                                                 MyProject   libgmp.a(mullo_n.o)                                 1   
Error   LNK2001     unresolved external symbol ___chkstk_ms                                                 MyProject   libgmp.a(mul_n.o)                                   1   
Error   LNK2001     unresolved external symbol ___chkstk_ms                                                 MyProject   libgmp.a(nussbaumer_mul.o)                          1   
Error   LNK2001     unresolved external symbol ___chkstk_ms                                                 MyProject   libgmp.a(sqr.o)                                     1   
Error   LNK2001     unresolved external symbol ___chkstk_ms                                                 MyProject   libgmp.a(toom42_mul.o)                              1   
Error   LNK2001     unresolved external symbol ___chkstk_ms                                                 MyProject   libgmp.a(toom53_mul.o)                              1   
Error   LNK2019     unresolved external symbol __mingw_raise_matherr referenced in function cos             MyProject   libmingwex.a(lib64_libmingwex_a-cos.o)              1   
Error   LNK2001     unresolved external symbol ___chkstk_ms                                                 MyProject   libmingwex.a(lib64_libmingwex_a-mingw_pformat.o)    1   
Error   LNK2001     unresolved external symbol __mingw_raise_matherr                                        MyProject   libmingwex.a(lib64_libmingwex_a-sin.o)              1   
Error   LNK2001     unresolved external symbol __mingw_raise_matherr                                        MyProject   libmingwex.a(lib64_libmingwex_a-sqrt.o)             1   

I imagine that I need to link my VS project with a static-library from MSYS2 but I canno't figure which one. Following this topic I already linked with C:\msys64\mingw64\x86_64-w64-mingw32\lib\libmingwex.a but now I'm quite stuck. Any suggestion?

Upvotes: 2

Views: 1542

Answers (1)

David Macek
David Macek

Reputation: 927

___chkstk_ms is defined in libgcc (...\msys64\mingw64\lib\gcc\x86_64-w64-mingw32\10.1.0\libgcc.a).

__mingw_raise_matherr is defined in libmingw32 (...\msys64\mingw64\x86_64-w64-mingw32\lib\libmingw32.a).

I don't know if this will help you get a working executable though. :) C++ is out of the question, but it seems you haven't bumped into that particular issue so far.

You can find these symbols using simple substring search and confirm using nm (T stands for a defined symbol, U for an undefined one). You can also call GCC with verbosity turned up to see the exact linker command behind the scenes, or call gcc -dumpspecs to read exactly when is what linked in.

Upvotes: 3

Related Questions