Using mingw and libtool to cross-compile static libraries


Mingw is a C/C++ compiler tool chain that can be used to cross-compile applications and libraries for the Windows operating system from a linux/unix build system. This article explains how to use mingw and GNU autotools (automake,autoconf,libtool) to build a static library that can be used in Windows.


Install mingw32

Under Debian compatible distributions, this is done by issuing :

apt-get install mingw32*

The mingw libraries and binaries will be installed in /usr/i586-mingw32msvc/ and in /usr/bin/


Makefile.am

In this file, we specify the source files for the library and any dependencies. In this example, the Makefile.am looks as follows :

lib_LTLIBRARIES    =    libtest.la
libtest_la_SOURCES =    file1.c \
                        file2.c

libtest_la_LDFLAGS =   -version-info 1:0:0

include_HEADERS    =	test.h

# Optionally Specify where to find external include files and libraries

AM_CFLAGS = -L../pcap/Lib/ -I../pcap/include/

Note that external libraries must be .lib, .a or .la. As for dlls, they can be processed using mingw-utilities to create .lib import libraries.


configure.ac

The configure.ac file performs checks on the availability of headers and libraries, then create makefiles. All what you see in this file are calls to M4 macros that do the checking.

AC_INIT(libtest, 0.0.1, e-mail@example.com)
AM_INIT_AUTOMAKE(libtest, 0.0.1)
AC_CONFIG_SRCDIR([file1.c])
AC_CONFIG_HEADER(config.h)

# Checks for programs.
AC_PROG_CXX
AC_MINGW32
AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_DISABLE_SHARED
AC_PROG_LIBTOOL


# Checks for header files.
AC_HEADER_STDC

AC_CHECK_HEADERS([windows.h stdlib.h string.h unistd.h], [],
[ echo "missing header"  exit -1])


# Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_INT32_T
AC_TYPE_INT8_T
AC_TYPE_SIZE_T
AC_TYPE_UINT8_T

# Checks for library functions.
AC_FUNC_CHOWN
AC_FUNC_FORK
AC_FUNC_MALLOC
AC_FUNC_REALLOC
AC_CHECK_FUNCS([ftruncate getpagesize memset munmap strerror])
AC_CHECK_FUNCS([memset])

AC_CONFIG_FILES([Makefile])
AC_OUTPUT

Prepare the build environment

Now, we need to create the configure script and Makefile.in files. The Makefile.in files are produced from Makefile.am files and contain place holders that will be changed by the configure script to create final Makefiles.

libtoolize --force
aclocal
autoconf
autoheader
automake -a -i -c --gnu

Configure and create makefiles

When the target platform (Windows, Bsd, Solaris, etc..) is specified with the —host option, the configure script will search for the cross-compiling suite for the specified platform. Cross-compilation tools commonly have their target architecture as prefix of their name. For instance the cross-compiler for windows (MinGW32) has its binaries called i586-mingw32msvc-gcc, i586-mingw32msvc-ld, i586-mingw32msvc-as, etc. Thus, to prepare the Makfiles for the build target 'Windows32' you would issue the following command.

./configure --host=i586-mingw32msvc

Build the static library

Now that the Makefiles are ready, we can build our library.

make


Labels: , , Wireless Internet Security Coding Network Monitoring

Comment

Enter your comment (wiki syntax is allowed):
TMVBY

Wireless Internet Security Performance RADIUS server Wireless Internet Security Performance RADIUS server