linux编译调用静态库

来源:互联网 发布:linux ntp时间对时 编辑:程序博客网 时间:2024/06/03 10:30

库testlib.c

#include <stdio.h>


int testlib()
{
printf("this is a lib");
}


程序test.c

#include <stdio.h>


int testlib();


int main()
{
printf("hello world \r\n");


testlib();


getchar();


return 0;
}



编写如下build.sh文件执行即可

#!/bin/bash



declare curdir=$(pwd)


gcc -c $curdir/testlib.c -o testlib.o
ar cr testlib.lib testlib.o
rm testlib.o


gcc $curdir/test.c -o Autotest.exe -L. testlib.lib

./Autotest.exe


//testlib.lib是静态库的名字,一般后缀为.a,名字以lib开头可以写为libtest.a

//这样调用的时候还可以写为gcc $curdir/test.c -o Autotest.exe -L. -ltest

//即libtest.a的调用可以写为-ltest

0 0