您的位置:首页 > 运维架构 > Linux

linux编译调用动态库

2015-09-23 16:09 495 查看
库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或者其他路径。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: