简单的例子:
我们使用strip.c、procpath.c创建动态库libstrip.dll
然后编译ah.c并连接libstrip.lib产生执行程序ah.exe
Makefile.am如下:
lib_LTLIBRARIES=libstrip.la
libstrip_la_SOURCES=strip.c procpath.c
libstrip_la_LDFLAGS=-no-undefined -avoid-version
bin_PROGRAMS=ah
ah_SOURCES=ah.c strip.h
ah_LDADD=libstrip.la
下面我逐条解释含义:
lib_LTLIBRARIES=libstrip.la
表示我们要创建一个库libstrip.la(一种抽象的库,根据不同的系统和参数它可以是一个静态库也可以是动态库,可以是.a,.lib,.dll)
libstrip_la_SOURCES=strip.c procpath.c
表示创建libstrip.la需要的源文件
libstrip_la_LDFLAGS=-no-undefined -avoid-version
链接的时候需要的链接产生。
-avoid-version告诉libtool不产生版本号。
-no-undefined告诉libtool可以产生一个dll,如果不加该参数将不会产生dll。
bin_PROGRAMS=ah
ah_SOURCES=ah.c strip.h
ah_LDADD=libstrip.la
这表示我们要产生一个执行程序,并且要链接libstrip。
这些文件都准备好了可以运行autoreconf命令来产生configure脚本。
autoreconf --install
下面我们看看怎么用vc来编译它,首先你需要cygwin,visual stdio 20xx,然后自己做一个脚本用来将cygwin-msvc-gcc转到vc的编译器cl.exe。
cygwin-msvc-gcc
#!/bin/sh
echo "cl $*" >> $0.log
/cygdrive/E/Microsoft\ Visual\ Studio\ 12.0/VC/bin/cl $* >> $0.log
将脚本放入到PATH中,并且确保你设置了vc的环境变量INCLUDE,LIBS前者用来指向vc的头文件目录,后者指向vc的库文件。确保cygwin-msvc-gcc可以真确的编译后继续。
当然你需要一个configure.ac文件。
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT([apphook], [0.1], [udsn2@sina.com])
AC_CONFIG_MACRO_DIR([m4])
AM_INIT_AUTOMAKE([foreign -Wall -Werror])
AC_CONFIG_SRCDIR([src/ah.c])
AC_CONFIG_HEADERS([config.h])
AM_PROG_AR
LT_PREREQ([2.4.0])
LT_INIT([disable-static win32-dll dlopen])
AC_PROG_LIBTOOL
# Checks for programs.
AC_PROG_CC
# Checks for libraries.
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AC_CONFIG_FILES([Makefile src/Makefile po/Makefile.in])
AC_OUTPUT
具体的含义不解释了,详细见”configure.ac的编写“。
下面就可以开始编译了
./configure --host=cygwin-msvc
make
我将编译过程中执行的命令都俘获下来,它看上去像下面这样:
../libtool --tag=CC --mode=compile /cygdrive/g/source/apphook/compile cygwin-msvc -gcc -DHAVE_CONFIG_H -I. -I.. -DDLL_EXPORT -DWIN32 -g -c -o strip.lo strip.c
cl -DHAVE_CONFIG_H -I. -I.. -DDLL_EXPORT -DWIN32 -g -c strip.c -DDLL_EXPORT -DPIC
./libtool --tag=CC --mode=compile /cygdrive/g/source/apphook/compile cygwin-msvc -gcc -DHAVE_CONFIG_H -I. -I.. -DDLL_EXPORT -DWIN32 -g -c -o procpath.lo procpath.c
cl -DHAVE_CONFIG_H -I. -I.. -DDLL_EXPORT -DWIN32 -g -c procpath.c -DDLL_EXPORT -DPIC
./libtool --tag=CC --mode=link /cygdrive/g/source/apphook/compile cygwin-msvc-gc c -g -no-undefined -avoid-version -o libstrip.la -rpath /usr/local/lib strip.lo procpath.lo
cl -o .libs/libstrip.dll .libs/strip.obj .libs/procpath.obj -link -dll
./libtool --tag=CC --mode=link /cygdrive/g/source/apphook/compile cygwin-msvc-gcc -o ah.exe ah.obj libstrip.la
cl -g -o .libs/ah.exe ah.obj ./.libs/libstrip.lib
按说cl不应该能识别-g -o -dll -link等产生,并产生正确的结果。但是事实上这个确实可以工作。根据稳妥的做法是在cygwin-msvc-gcc中做参数翻译工作。