当前位置: 技术问答>linux和unix
如何编写动态库的makefile
来源: 互联网 发布时间:2016-02-27
本文导语: 我有一个.CPP和.h的文件,现在想把其编译成动态库的形式,各位大侠知道应该如何编写这个makefile文件,我是在嵌入式下开发。盼回复 | CC = gcc CXX = gcc CFLAGS ...
我有一个.CPP和.h的文件,现在想把其编译成动态库的形式,各位大侠知道应该如何编写这个makefile文件,我是在嵌入式下开发。盼回复
|
CC = gcc
CXX = gcc
CFLAGS =
CXXFLAGS=
LINK = gcc
LFLAGS = -shared
LIBS =
####### Files
HEADERS = s1.h
SOURCES = s1.c
OBJECTS = s1.o
TARGET = s1.so
####### Implicit rules
.SUFFIXES: .cpp .cxx .cc .C .c
.cpp.o:
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $
/dev/null 2>&1
mv -f .deps/mym.Tpo .deps/mym.Plo
/usr/local/bin/bash ../libtool --tag=CXX --mode=link g++ -g -O2 -o libmym.la -rpath /usr/home/mymtom/local/lib mym.lo
g++ -shared -nostdlib /usr/lib/crti.o /usr/lib/crtbeginS.o .libs/mym.o -L/usr/lib -lstdc++ -lm -lc -lgcc_s /usr/lib/crtendS.o /usr/lib/crtn.o -Wl,-soname -Wl,libmym.so.0 -o .libs/libmym.so.0
(cd .libs && rm -f libmym.so && ln -s libmym.so.0 libmym.so)
(cd .libs && rm -f libmym.so && ln -s libmym.so.0 libmym.so)
ar cru .libs/libmym.a mym.o
ranlib .libs/libmym.a
creating libmym.la
(cd .libs && rm -f libmym.la && ln -s ../libmym.la libmym.la)
gmake[2]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0/libmym'
gmake[2]: Entering directory `/usr/home/mymtom/src/play/automake/hello-1.0'
gmake[2]: Nothing to be done for `all-am'.
gmake[2]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0'
gmake[1]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0'
$ gmake install
Making install in libmym
gmake[1]: Entering directory `/usr/home/mymtom/src/play/automake/hello-1.0/libmym'
gmake[2]: Entering directory `/usr/home/mymtom/src/play/automake/hello-1.0/libmym'
test -z "/usr/home/mymtom/local/lib" || .././install-sh -c -d "/usr/home/mymtom/local/lib"
/usr/local/bin/bash ../libtool --mode=install /usr/bin/install -c 'libmym.la' '/usr/home/mymtom/local/lib/libmym.la'
/usr/bin/install -c .libs/libmym.so.0 /usr/home/mymtom/local/lib/libmym.so.0
(cd /usr/home/mymtom/local/lib && { ln -s -f libmym.so.0 libmym.so || { rm -f libmym.so && ln -s libmym.so.0 libmym.so; }; })
(cd /usr/home/mymtom/local/lib && { ln -s -f libmym.so.0 libmym.so || { rm -f libmym.so && ln -s libmym.so.0 libmym.so; }; })
/usr/bin/install -c .libs/libmym.lai /usr/home/mymtom/local/lib/libmym.la
/usr/bin/install -c .libs/libmym.a /usr/home/mymtom/local/lib/libmym.a
chmod 644 /usr/home/mymtom/local/lib/libmym.a
ranlib /usr/home/mymtom/local/lib/libmym.a
----------------------------------------------------------------------
Libraries have been installed in:
/usr/home/mymtom/local/lib
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,--rpath -Wl,LIBDIR' linker flag
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
gmake[2]: Nothing to be done for `install-data-am'.
gmake[2]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0/libmym'
gmake[1]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0/libmym'
gmake[1]: Entering directory `/usr/home/mymtom/src/play/automake/hello-1.0'
gmake[2]: Entering directory `/usr/home/mymtom/src/play/automake/hello-1.0'
gmake[2]: Nothing to be done for `install-exec-am'.
gmake[2]: Nothing to be done for `install-data-am'.
gmake[2]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0'
gmake[1]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0'
$ gmake dist-bzip2
{ test ! -d libmym-1.0 || { find libmym-1.0 -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr libmym-1.0; }; }
test -d libmym-1.0 || mkdir libmym-1.0
list='libmym'; for subdir in $list; do
if test "$subdir" = .; then :; else
test -d "libmym-1.0/$subdir"
|| ./install-sh -c -d "libmym-1.0/$subdir"
|| exit 1;
distdir=`CDPATH="${ZSH_VERSION+.}:" && cd libmym-1.0 && pwd`;
top_distdir=`CDPATH="${ZSH_VERSION+.}:" && cd libmym-1.0 && pwd`;
(cd $subdir &&
gmake
top_distdir="$top_distdir"
distdir="$distdir/$subdir"
am__remove_distdir=:
am__skip_length_check=:
distdir)
|| exit 1;
fi;
done
gmake[1]: Entering directory `/usr/home/mymtom/src/play/automake/hello-1.0/libmym'
gmake[1]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0/libmym'
find libmym-1.0 -type d ! -perm -777 -exec chmod a+rwx {} ; -o
! -type d ! -perm -444 -links 1 -exec chmod a+r {} ; -o
! -type d ! -perm -400 -exec chmod a+r {} ; -o
! -type d ! -perm -444 -exec /usr/local/bin/bash /usr/home/mymtom/src/play/automake/hello-1.0/install-sh -c -m a+r {} {} ;
|| chmod -R a+r libmym-1.0
tardir=libmym-1.0 && /usr/local/bin/bash /usr/home/mymtom/src/play/automake/hello-1.0/missing --run tar chof - "$tardir" | bzip2 -9 -c >libmym-1.0.tar.bz2
{ test ! -d libmym-1.0 || { find libmym-1.0 -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr libmym-1.0; }; }
$
CXX = gcc
CFLAGS =
CXXFLAGS=
LINK = gcc
LFLAGS = -shared
LIBS =
####### Files
HEADERS = s1.h
SOURCES = s1.c
OBJECTS = s1.o
TARGET = s1.so
####### Implicit rules
.SUFFIXES: .cpp .cxx .cc .C .c
.cpp.o:
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $
/dev/null 2>&1
mv -f .deps/mym.Tpo .deps/mym.Plo
/usr/local/bin/bash ../libtool --tag=CXX --mode=link g++ -g -O2 -o libmym.la -rpath /usr/home/mymtom/local/lib mym.lo
g++ -shared -nostdlib /usr/lib/crti.o /usr/lib/crtbeginS.o .libs/mym.o -L/usr/lib -lstdc++ -lm -lc -lgcc_s /usr/lib/crtendS.o /usr/lib/crtn.o -Wl,-soname -Wl,libmym.so.0 -o .libs/libmym.so.0
(cd .libs && rm -f libmym.so && ln -s libmym.so.0 libmym.so)
(cd .libs && rm -f libmym.so && ln -s libmym.so.0 libmym.so)
ar cru .libs/libmym.a mym.o
ranlib .libs/libmym.a
creating libmym.la
(cd .libs && rm -f libmym.la && ln -s ../libmym.la libmym.la)
gmake[2]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0/libmym'
gmake[2]: Entering directory `/usr/home/mymtom/src/play/automake/hello-1.0'
gmake[2]: Nothing to be done for `all-am'.
gmake[2]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0'
gmake[1]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0'
$ gmake install
Making install in libmym
gmake[1]: Entering directory `/usr/home/mymtom/src/play/automake/hello-1.0/libmym'
gmake[2]: Entering directory `/usr/home/mymtom/src/play/automake/hello-1.0/libmym'
test -z "/usr/home/mymtom/local/lib" || .././install-sh -c -d "/usr/home/mymtom/local/lib"
/usr/local/bin/bash ../libtool --mode=install /usr/bin/install -c 'libmym.la' '/usr/home/mymtom/local/lib/libmym.la'
/usr/bin/install -c .libs/libmym.so.0 /usr/home/mymtom/local/lib/libmym.so.0
(cd /usr/home/mymtom/local/lib && { ln -s -f libmym.so.0 libmym.so || { rm -f libmym.so && ln -s libmym.so.0 libmym.so; }; })
(cd /usr/home/mymtom/local/lib && { ln -s -f libmym.so.0 libmym.so || { rm -f libmym.so && ln -s libmym.so.0 libmym.so; }; })
/usr/bin/install -c .libs/libmym.lai /usr/home/mymtom/local/lib/libmym.la
/usr/bin/install -c .libs/libmym.a /usr/home/mymtom/local/lib/libmym.a
chmod 644 /usr/home/mymtom/local/lib/libmym.a
ranlib /usr/home/mymtom/local/lib/libmym.a
----------------------------------------------------------------------
Libraries have been installed in:
/usr/home/mymtom/local/lib
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,--rpath -Wl,LIBDIR' linker flag
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
gmake[2]: Nothing to be done for `install-data-am'.
gmake[2]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0/libmym'
gmake[1]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0/libmym'
gmake[1]: Entering directory `/usr/home/mymtom/src/play/automake/hello-1.0'
gmake[2]: Entering directory `/usr/home/mymtom/src/play/automake/hello-1.0'
gmake[2]: Nothing to be done for `install-exec-am'.
gmake[2]: Nothing to be done for `install-data-am'.
gmake[2]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0'
gmake[1]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0'
$ gmake dist-bzip2
{ test ! -d libmym-1.0 || { find libmym-1.0 -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr libmym-1.0; }; }
test -d libmym-1.0 || mkdir libmym-1.0
list='libmym'; for subdir in $list; do
if test "$subdir" = .; then :; else
test -d "libmym-1.0/$subdir"
|| ./install-sh -c -d "libmym-1.0/$subdir"
|| exit 1;
distdir=`CDPATH="${ZSH_VERSION+.}:" && cd libmym-1.0 && pwd`;
top_distdir=`CDPATH="${ZSH_VERSION+.}:" && cd libmym-1.0 && pwd`;
(cd $subdir &&
gmake
top_distdir="$top_distdir"
distdir="$distdir/$subdir"
am__remove_distdir=:
am__skip_length_check=:
distdir)
|| exit 1;
fi;
done
gmake[1]: Entering directory `/usr/home/mymtom/src/play/automake/hello-1.0/libmym'
gmake[1]: Leaving directory `/usr/home/mymtom/src/play/automake/hello-1.0/libmym'
find libmym-1.0 -type d ! -perm -777 -exec chmod a+rwx {} ; -o
! -type d ! -perm -444 -links 1 -exec chmod a+r {} ; -o
! -type d ! -perm -400 -exec chmod a+r {} ; -o
! -type d ! -perm -444 -exec /usr/local/bin/bash /usr/home/mymtom/src/play/automake/hello-1.0/install-sh -c -m a+r {} {} ;
|| chmod -R a+r libmym-1.0
tardir=libmym-1.0 && /usr/local/bin/bash /usr/home/mymtom/src/play/automake/hello-1.0/missing --run tar chof - "$tardir" | bzip2 -9 -c >libmym-1.0.tar.bz2
{ test ! -d libmym-1.0 || { find libmym-1.0 -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr libmym-1.0; }; }
$
|
目标指定为XXX.so
编译的时候加-shared -fpic
编译的时候加-shared -fpic