linux so

来源:互联网 发布:黑金 知乎 编辑:程序博客网 时间:2024/05/29 12:55

1、动态库的编译

testa.c

#include <stdio.h>#include "test.h"void TestA(){    printf("TestA func\n");}

testb.c
#include <stdio.h>#include "test.h"void TestB(){    printf("TestB func\n");}

gcc testa.c testb.c -fPIC -shared  -o libtest.so

2、动态库的链接,生成执行文件


main.c

#include "test.h"int main(int argc, char *argv[]){        TestA();        TestB();        return 0;}

gcc main.o -L. -ltest -o $@

.PHONY:all cleanCC=gccCFLAGS=-Wall -gSO=libtest.soBIN=mainall:$(SO) $(BIN)%.o:%.c        $(CC) $(CFLAGS) -c $< -o  $@$(SO):testa.c testb.c        $(CC) $^ -fPIC -shared  -o $@$(BIN):main.o        $(CC) $< -L. -ltest -o $@clean:        rm -rf *.o *.so


0 0