库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 $curdir/testlib.c -fPIC -shared -o libtest.so
sudo mv libtest.so /lib
gcc $curdir/test.c -o Autotest.exe -L. -ltest
./Autotest.exe
也可以
gcc $curdir/testlib.c -fPIC -shared -o testlib.dll
sudo cp testlib.dll /lib
gcc $curdir/test.c -o Autotest.exe -L. testlib.dll
./Autotest.exe
之所以要sudo mv libtest.so /lib是因为动态库目前没有指定路径,如果确定了动态库的路径可以直接gcc $curdir/test.c -o Autotest.exe -L. ./testlib.dll或者其他路径。