linux下静态库使用

来源:互联网 发布:奶粉 保鲜袋 知乎 编辑:程序博客网 时间:2024/06/06 05:41

//test.c

#include <stdio.h>

Test(void)

{ printf("Test ok...\n");}


//test.h

#ifndef _TEST_H

#define _TEST_H

void Test(void)

#endif//_TEST_H


//main.c

#include <stdio.h>

#include "test.h"

int main(int argc, char *argv[])

{

Test();

return 0;

}

生成静态库步骤:

1编译: gcc -o test.o test.c

2生成库文件: ar - ./ libtest.a test.o

3使用:

#makefile

main: main.o

         gcc -o $@ -L./ $< -lTest

main.o: main.c

        gcc -I./ -o $@ -c $<

clean:

         rm *.o main