作业系统: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
只是插入给定文件的内容,因此它应该是源代码。您必须通过将库作为参数传递给链接器来链接您的库。
阅读here了解更多。