如何在c中调用动态库?

时间:2015-08-09 18:49:40

标签: c linux gcc dll

操作系统:ubuntu 14.04

编写两个名为dtest1.c和dtest2.c的.c文件

dtest1.c

int p = 2;
void print()
{
    printf("this is the first dll test!\n");

    }

dtest2.c

int p = 3;
void print()
{
    printf("this is the second dll test!\n");

    }

然后,编译它们以获得两个名为dtest1.so和dtest2.so

的文件
  $gcc -O -fpic -shared -o dtest1.so dtest1.c 
  $gcc -O -fpic -shared -o dtest2.so dtest2.c 

编写名为dtest3.c的.c文件

dtest3.c

#include "dtest1.so"
int main ()
{
    print();
    return 0;
    }

到目前为止,everthing很好。没有错误(只有警告

然后:

gcc -o dtest3 dtest3.c dtest1.so

错误:

In file included from dtest3.c:1:0:
dtest1.so:17:1: warning: null character(s) ignored [enabled by default]
dtest1.so:17:2: error: stray ‘\260’ in program
   ......
   ......    /*omit too many similar information */
dtest1.so:18:2: error: stray ‘\212’ in program
dtest1.so:18:2: error: stray ‘\1’ in program
In file included from dtest3.c:1:0:
dtest1.so:18:956: warning: null character(s) ignored [enabled by default]
In file included from dtest3.c:1:0:
dtest1.so:18:2: error: stray ‘\244’ in program
dtest1.so:18:2: error: stray ‘\1’ in program
In file included from dtest3.c:1:0:
dtest1.so:18:980: warning: null character(s) ignored [enabled by default]
dtest1.so:18:982: warning: null character(s) preserved in literal [enabled by default]
dtest1.so:18:982: warning: missing terminating " character [enabled by default]
In file included from dtest3.c:1:0:
dtest1.so:18:2: error: missing terminating " character

那有什么问题?

请帮我找出错误,谢谢。

2 个答案:

答案 0 :(得分:2)

库是编译代码,而不是(文本)源代码。 #include只是插入给定文件的内容,因此它应该是源代码。您必须通过将其作为参数传递给链接器来链接您的库。

阅读here了解更多信息。

答案 1 :(得分:-2)

感谢大家帮我解决,并编写过程。

写一个 .h 文件:

<强> dtest1.h

void print()
{
    printf("this is the first dll test!(this is a .h file)\n");
}

修改 dtest3.c 文件,删除#include &#34; dtest1.so&#34; 添加 a

#include "dtest1.h"

并且完成了:

gcc -o dtest3 dtest3.c dtest1.so 

给我一​​些信息:

In file included from dtest3.c:1:0:
dtest1.h: In function ‘print’:
dtest1.h:3:5: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
     printf("this is the fist dll test!(this is a .h file)\n");
     ^

尝试运行dtest3:

./dtest3

获取这样的信息:

this is the fist dll test!(this is a .h file)

但我真的很困惑,所以再试一次:

$ vi dtest1.h

修改文件:

<强> dtest1.h

void print();

并修改dtest1.c:

$ vi dtest1.c 

添加一行:

#include "dtest1.h"

并将其复制以获取新的.so文件:

gcc -O -fpic -shared -o dtest1.so dtest1.c

简化了dtest3.c文件:

gcc -o dtest3 dtest3.c dtest1.so 

没有警告没有错误.....很开心

$ ./dtest3
./dtest3: error while loading shared libraries: dtest1.so: cannot open shared object file: No such file or directory

新问题.... T_T,但谷歌搜索的结果告诉我:

系统无法找到库,即使我提供dtest1.so的路径,当我完成dtest3.c时,但这些信息没有写入dtest3

做一些测试:

$ ldd dtest3

/ 此命令将告诉文件所依赖的库 /

linux-vdso.so.1 =>  (0x00007fff1d3d7000)
dtest1.so => not found
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f6e9914c000)
/lib64/ld-linux-x86-64.so.2 (0x00007f6e99532000)

/ 悲伤充斥着我的思绪...... /

但网页告诉我一种方法来解决它:

$export LD_LIBRARY_PATH=.

系统会在第一时间找到该路径,因此可以确保找到dtest1.so。

现在再试一次:

$ ./dtest3

this is the first dll test!

<强>恭喜!

相关问题