感谢您的所有回复。我有一个似乎正在工作的解决方案。 下面是一个例子的详细问题。
main.c中,我们有:
#include <stdio.h>
extern int foo();
int bar()
{
printf("bar in main.c called\n");
return 0;
}
int main()
{
printf("result from foo is %d\n", foo());
printf("result from bar is %d\n", bar());
}
在foo.c的有:
extern int bar();
int foo()
{
int x = bar();
return x;
}
在bar.c,我们有:
#include <stdio.h>
int bar()
{
printf("bar in bar.c called\n");
return 2;
}
编译bar.c和foo.c的:
$ gcc -fPIC -c bar.c
$ gcc -fPIC -c foo.c
添加文件bar.o到静态库:
$ ar r libbar.a bar.o
现在创建使用文件foo.o并链接与静态共享库搜索libbar.a
$ gcc -shared -o libfoo.so foo.o -L. -lbar
编译main.c中与共享库链接libfoo.so
$ gcc -o main main.c -L. -lfoo
设置LD_LIBRARY_PATH以查找libfoo。所以并运行主:
$ setenv LD_LIBRARY_PATH `pwd`
$ ./main
bar in main.c called
result from foo is 0
bar in main.c called
result from bar is 0
请注意,在main.c中栏的版本叫做,没有连接到共享库的版本。
在main2.c我们:
#include <stdio.h>
#include <dlfcn.h>
int bar()
{
printf("bar in main2.c called\n");
return 0;
}
int main()
{
int x;
int (*foo)();
void *handle = dlopen("libfoo.so", RTLD_GLOBAL|RTLD_LAZY);
foo = dlsym(handle, "foo");
printf("result from foo is %d\n", foo());
printf("result from bar is %d\n", bar());
}
编译和运行main2.c(注意我们不需要明确地libfoo.so链接):
$ gcc -o main2 main2.c -ldl
$ ./main2
bar in bar.c called
result from foo is 2
bar in main2.c called
result from bar is 0
现在foo的共享共享库中的库调用栏和main.c中的主要调用栏。我不认为这种行为是直观的,使用dlopen/dlsym更多的工作,但它确实解决了我的问题。
再次感谢您的评论。
你可以让你的应用动态链接到VENDOR1吗? – 2010-07-12 23:31:32
不以任何方式与语言无关。这对于编译器链接器和操作系统来说非常具体,它们是如何一起工作的。最简单的方法是通过电子邮件向供应商和他们如何解决这个问题。 – 2010-07-12 23:31:43
我们目前的想法是,至少在linux上,使用libbar.so上的dlopen()和RTLD_DEEPBIND标志。另一种可能性是将libfoo.a的应用程序分离到共享库中,libbaz.so将libfoo.a封装起来,然后将应用程序dlopen libbaz.so和libbar.so与RTLD_LOCAL分离,我们认为这可能会保留所有重复的符号在内部。这可能适用于Linux,但我们需要它,因此也适用于Solaris,AIX和HPUX。 – YerBlues 2010-07-13 00:05:42