2

I'm trying to build CGAL for windows to use in my project under Visual Studio 2010. CGAL requires GMP and MPFR libraries and provides them in distribution. However, it provides them as a lib+dll bundle, while I want them to be compiled statically in form of a single .lib file.

So now I'm trying to build GMP and MPFR as a static library under windows. I'm using cygwin for this purpose as suggested here. After call to configure and make I have output libs with .a extension with additional .la file. I don't really know much about static libraries for Unix, so I suggested it is the same as .lib with just different extension. I renamed it to .lib and linked to my project - it fitted well.

The first question: was I correct doing so? Are the .a and .lib files really the same? I saw this question, but didn't find it useful enough.

Then problem arose: I had

error LNK2019: unresolved external symbol ___getreent referenced in function ___gmp_default_reallocate 

Seems like some cygwin functions are not linked in resulting gmp.lib. I found here, that getreent may be exported from libcygwin.a. So I copied it to libcygwin.lib and linked to my project. Not surprisingly, I received the following error:

error LNK2005: _strcpy already defined in libcygwin.lib(t-d001719.o) in libcmtd.lib(strcat.obj) 

Of course, I cannot know what functions and how are declared in this library and seems like strcpy is conflicting with one from Visual Studio. What I really want to happen is gmp.lib would be smart enough to link this function statically. So,

The second question: how to force GMP to link library dependencies? or How to properly build GMP for windows without using cygwin?

1 Answer 1

3

UPDATE: See the MPIR project page for the answer to all your problems (it allows you to build MPIR, a GMP compatible library, and MPFR with Visual Studio). The new MPIR homepage is located here, but lacks the MPFR info as far as I can tell.


You don't want Cygwin.

You need a Bash shell and MinGW(.org/-w64). If you build with the cygwin compiler, you need to link to the Cygwin DLL, which in your case is stupid, as GMP and MPFR both are buildable by MinGW.

The only thing is: I don't believe either library is buildable by MSVC, and MSVC can't link with MinGW libraries (this is exactly the reason your project's authors bundled DLL's and import libraries), so you'll need to build everything with MinGW GCC or use the DLL's.


Below are instructions for building a GCC static library of GMP and MPFR.

In order to build GMP for Windows with MinGW-w64 GCC, you'll need MSYS. Unzip this somewhere like C:\Dev\msys so that C:\Dev\msys\bin\sh.exe is present.

Then you'll need a MinGW-w64 GCC:

(I recommend my "Personal Builds", especially the 4.6.3-1 package. Download the 4.6.3-1-gcc_rubenvb.7z package, but any other MinGW bundle should do for this)

Unzip that to say C:\Dev\mingw64 so that C:\Dev\mingw64\bin\gcc.exe exists.

Double click on C:\Dev\msys\msys.bat. Type:

export PATH=/c/Dev/mingw64/bin:$PATH 

And press enter. Unzip the GMP and MPFR source to your /home/USERNAME/* directory, make build directories, and remember to use for configure:

--enable-static --disable-shared --prefix=/easytemplibinstalldir 

along with

--host=i686-w64-mingw32 

for 32-bit or

--host=x86_64-w64-mingw32 

for 64-bit. I believe GMP also requires --build set to the same value. After configure finishes, type make, and then make install, optionally preceded by make check.

You should have libgmp.a in /easytemplibinstalldir/lib. For MPFR, add

--with-gmp=/easytemplibinstalldir 

to its configure line.

You will have to manually link both libgmp and libmpfr in the correct order for it to work, no automated dependency linking is possible on Windows for this.

Sign up to request clarification or add additional context in comments.

10 Comments

How can that be that MSVC (or whatever) cannot link .lib built with MinGW (or whoever)? I though .lib is compiler-independent.
@Mikhail oh no, it's not. MinGW GCC produces an .a file, no .lib involved. MSVC doesn't care about GCC's output format, why should it be able to link? GCC on the other hand can link to 32-bit .lib files produced by MSVC, because some GCC/binutils dev cared enough to make it possible at all.
So, you mean there is now way to build GMP to static .lib under Windows at all? That's pretty confusing. Why am I able to link gmp.a file produced by cygwin with MSVC?
@Mikhail I don't know of a way to build GMP with MSVC. There's nothing confusing about that: GMP is a GNU project, uses C99, and MSVC doesn't get along with either of them. I guess there is limited compatibility in the file formats (see here for example), but linking Cygwin code with MSVC is asking for trouble. See update for easily googleable information.
@xamid the triplet x86_64-w64-mingw64 does not exist. The triplet is built up as architecture-vendor-os. In this case the "os" is mingw32, which due to historical reasons means the Win32 API as GCC sees it. This Win32 API is the same for 64-bit and 32-bit Windows. The w64 comes from MinGW-w64, which provides the C runtime on which this compiler is built. The 32-bit compiler, which is the one you downloaded, has triplet i686-w64-mingw32, where the first bit, i686, means 32-bit.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.