creating hello
-rpath项负责添加运行时库路径,否则只能手工修改LD_LIBRARY_PATH环境变量了。
验证一下:
# ldd .libs/hello
linux-gate.so.1 => (0xffffe000)
libhello.so.0 => /usr/local/lib/libhello.so.0 (0x40019000)
libc.so.6 => /lib/tls/libc.so.6 (0x40031000)
/lib/ld-linux.so.2 (0x40000000)
5.install executable file
#libtool --mode=install cp hello /usr/local/bin/hello
安装可执行程序。
6.运行
libtool --mode=execute hello
或直接运行hello
注意:此处hello已经安装在/usr/local/bin下了,可以用which hello来查看
【附】源码
foo.c
#include <stdio.h>
char msg[128]="Hello world";
void print()
{
printf("%s\n", msg);
}
hello.c:
extern char msg[128];
extern void print();
int main()
{
print();
}
Makefile:
LO_OBJS = foo.lo
PACKAGE_VERSION = 1:1:1
LIBDIR=/usr/local/lib
BINDIR=/usr/local/bin
all : hello
install : libhello.la hello
libtool --mode=install install -c libhello.la
${LIBDIR}/libhello.la
libtool --mode=install cp hello ${BINDIR}/hello
uninstall : ${LIBDIR}/libhello.la ${BINDIR}/hello
libtool --mode=uninstall /bin/rm ${LIBDIR}/libhello.la
libtool --mode=uninstall /bin/rm ${BINDIR}/hello
hello : libhello.la hello.lo
libtool --mode=install install -c libhello.la ${LIBDIR}/libhello.la
libtool --mode=link gcc -g -O -o hello hello.lo -rpath ${LIBDIR} libhello.la
libhello.la : $(LO_OBJS)
libtool --mode=link --tag=CC gcc -g -O -o libhello.la $(LO_OBJS) -rpath ${LIBDIR} -version-info ${PACKAGE_VERSION}
foo.lo : foo.c
libtool --mode=compile gcc -g -O -c foo.c
hello.lo : hello.c
libtool --mode=compile gcc -g -O -c hello.c
clean :
rm -f lib*.a *~ *core *.lo *.o *.la hello
rm -rf .libs
这样,用户可以用make编译,make install/uninstall安装/卸载,make clean清除编译临时文件,安装成功后,可以直接执行hello,不必指明路径也不必再另设环境变量LD_LIBRARY_PATH,非常方便!
-rpath项负责添加运行时库路径,否则只能手工修改LD_LIBRARY_PATH环境变量了。
验证一下:
# ldd .libs/hello
linux-gate.so.1 => (0xffffe000)
libhello.so.0 => /usr/local/lib/libhello.so.0 (0x40019000)
libc.so.6 => /lib/tls/libc.so.6 (0x40031000)
/lib/ld-linux.so.2 (0x40000000)
5.install executable file
#libtool --mode=install cp hello /usr/local/bin/hello
安装可执行程序。
6.运行
libtool --mode=execute hello
或直接运行hello
注意:此处hello已经安装在/usr/local/bin下了,可以用which hello来查看
【附】源码
foo.c
#include <stdio.h>
char msg[128]="Hello world";
void print()
{
printf("%s\n", msg);
}
hello.c:
extern char msg[128];
extern void print();
int main()
{
print();
}
Makefile:
LO_OBJS = foo.lo
PACKAGE_VERSION = 1:1:1
LIBDIR=/usr/local/lib
BINDIR=/usr/local/bin
all : hello
install : libhello.la hello
libtool --mode=install install -c libhello.la
${LIBDIR}/libhello.la
libtool --mode=install cp hello ${BINDIR}/hello
uninstall : ${LIBDIR}/libhello.la ${BINDIR}/hello
libtool --mode=uninstall /bin/rm ${LIBDIR}/libhello.la
libtool --mode=uninstall /bin/rm ${BINDIR}/hello
hello : libhello.la hello.lo
libtool --mode=install install -c libhello.la ${LIBDIR}/libhello.la
libtool --mode=link gcc -g -O -o hello hello.lo -rpath ${LIBDIR} libhello.la
libhello.la : $(LO_OBJS)
libtool --mode=link --tag=CC gcc -g -O -o libhello.la $(LO_OBJS) -rpath ${LIBDIR} -version-info ${PACKAGE_VERSION}
foo.lo : foo.c
libtool --mode=compile gcc -g -O -c foo.c
hello.lo : hello.c
libtool --mode=compile gcc -g -O -c hello.c
clean :
rm -f lib*.a *~ *core *.lo *.o *.la hello
rm -rf .libs
这样,用户可以用make编译,make install/uninstall安装/卸载,make clean清除编译临时文件,安装成功后,可以直接执行hello,不必指明路径也不必再另设环境变量LD_LIBRARY_PATH,非常方便!