Libtool建立和使用linux下動態庫

來源:互聯網
上載者:User

 

檔案fun.c,fun.h,hello.c,hello.h,main.c動態庫函數都在fun.c和hello.c裡面
fun.c:

int add(int a, int b)
{
 return a+b;
}

fun.h:

#ifndef _FUN_H_11
#define _FUN_H_11
int add(int a, int b);
#endif
----------------------------

hello.c:

#i nclude <stdio.h>
void output(char *ss)
{
 printf("HELLO  %s/n", ss);
}

hello.h

#ifndef HELLO_H_111
#define HELLO_H_111
void output(char *ss);
#endif
----------------------------

main.c:

#i nclude <stdio.h>
#i nclude "hello.h"
#i nclude "fun.h"
void
main()
{
 output("world");
 printf("Test Value:%d/n", add(1, 2));
}

使用libtools建立和使用安裝動態庫步驟:

(1)
libtool --mode=compile gcc -g -O -c hello.c
libtool --mode=compile gcc -g -O -c fun.c
libtool --mode=compile gcc -g -O -c main.c
#產生各自的o檔案

(2)
libtool --mode=link gcc -g -O -o libhello.la hello.lo fun.lo -rpath /usr/local/lib -lm
#串連成動態庫檔案

(3)
libtool --mode=link gcc -g -O -o test main.o libhello.la -lm
#串連產生可執行檔test

(4)
libtool --mode=install cp libhello.la /usr/local/lib/libhello.la
libtool --mode=install install -c libhello.la /usr/local/lib/libhello.la
libtool -n --mode=finish /usr/local/lib
libtool install -c test /usr/local/bin/test
#安裝動態庫

然後就可以運行/usr/local/bin/test了,當然路徑可以任意設定,這是手動過程,寫成Makefile檔案為:

OBJS = fun.o hello.o
LO_OBJS = main.lo fun.lo hello.lo
PACKAGE_VERSION = 1:1:1
LIBDIR=/usr/local/lib

all : test

install : libhello.la
 
test : libhello.la main.o
 libtool --mode=link gcc -g -O -o test main.o libhello.la -lm

libhello.la : $(OBJS)
 libtool gcc -g -O -o libhello.la $(LO_OBJS) -rpath ${LIBDIR} -lm -version-info ${PACKAGE_VERSION}

main.o : main.c fun.h hello.h
 libtool --mode=compile gcc -g -O -c main.c

fun.o : fun.c fun.h
 libtool --mode=compile gcc -g -O -c fun.c

hello.o : hello.c hello.h
 libtool --mode=compile gcc -g -O -c hello.c

clean :
 @rm -f OBJ/* lib*.a *~ *core *.lo *.o *.la
 @rm -rf .libs

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.