库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