Mikhail
Mikhail

Reputation: 21749

Building static libraries for windows using cygwin

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?

Upvotes: 2

Views: 8965

Answers (1)

rubenvb
rubenvb

Reputation: 76519

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.

Upvotes: 3

Related Questions