【问题标题】:How to call a dynamic library in c?如何在c中调用动态库?
【发布时间】:2015-08-09 18:49:40
【问题描述】:

操作系统: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;
    }

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

然后:

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

这有什么问题?

请帮我找出错误,谢谢。

【问题讨论】:

  • 你不#include编译库,你#include C源代码,通常放在一个带有.h扩展名的文件中。
  • 谢谢你的话,你启发了我解决这个问题。^_^

标签: c linux gcc dll


【解决方案1】:

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

阅读here了解更多信息。

【讨论】:

  • 谢谢~但是页面很丑,信息对我来说太难了。^_^
  • @Lie Zang - 页面不“丑”,信息有价值。请阅读。 ALSO:请“点赞”和“接受”Youka 的回复。
  • @LieZang 那么我必须告诉你:回归基础。你可以在网上搜索,关于这个任务的教程还有很多,也许有一个适合你的进度。
  • @Youka 谢谢你的建议❤我会继续努力的O(∩_∩)O
  • @paulsm4 页面很丑╭(╯^╰)╮
【解决方案2】:

谢谢大家帮我解决,写下流程。

像这样写一个 .h 文件:

dtest1.h

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

修改dtest3.c文件,删除#include "dtest1.so"添加一个

#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

cimplited 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,但是google搜索的结果告诉我:

系统找不到该库,即使我在编译 dtest3.c 时提供了 dtest1.so 的路径,但此信息并未写入 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!

恭喜!

【讨论】:

  • 你可以简单地通过程序ldconfig更新系统的链接目录缓存而不是$export LD_LIBRARY_PATH=.
猜你喜欢
  • 2012-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-26
  • 1970-01-01
  • 1970-01-01
  • 2021-05-10
相关资源
最近更新 更多