一个库的多个版本链接 [英] Linking with multiple versions of a library

查看:204
本文介绍了一个库的多个版本链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态地库,libfoo时的X版本的链接,从第三方供应商,供应商1的应用程序。它还具有动态(共享)库链接,libbar的,来自不同供应商的第三方,VENDOR2,即从静态链接供应商1的libfoo的版本年。

I have an application that statically links with version X of a library, libfoo, from thirdparty vendor, VENDOR1. It also links with a dynamic (shared) library, libbar, from a different thirdparty vendor, VENDOR2, that statically links version Y of libfoo from VENDOR1.

所以libbar.so包含libfoo.a中的版本Y和我的可执行文件包含libfoo.a中的X版本
libbar的只使用libfoo的内部,有从我的应用程序li​​bbar的传递没有libfoo的对象。

So libbar.so contains version Y of libfoo.a and my executable contains version X of libfoo.a libbar only uses libfoo internally and there are no libfoo objects passed from my app to libbar.

有在制作的时候,但在运行时,应用程序故障赛格没有错误。究其原因似乎是该版本X使用具有不同的尺寸,他们的版本Y和运行时链接程序似乎混合了结构,它们习惯于由。

There are no errors at build time but at runtime the app seg faults. The reason seems to be that version X uses structures that have a different size they version Y and the runtime linker seems to be mixing up which get used by which.

这两个供应商1&放大器; VENDOR2是闭源的,所以我不能重建它们。

Both VENDOR1 & VENDOR2 are closed source so I cannot rebuild them.

有没有建/链接我的应用程序,使得它始终解析版本X和libbar的送花儿给人解析到版本Y和方式两个从来没有混?

Is there a way to build/link my app such that it always resolves to version X and libbar alway resolves to version Y and the two never mix?

推荐答案

感谢所有的答复。我有一个似乎工作的解决方案。
下面是详细的问题用一个例子。

Thanks for all the responses. I have a solution that seem to be working. Here's the problem in detail with an example.

在main.c中,我们有:

In main.c we have:

#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的,我们有:

In foo.c we have:

extern int bar();

int foo()
{
    int x = bar();
    return x;
}

在bar.c,我们有:

In bar.c we have:

#include <stdio.h>

int bar()
{
    printf("bar in bar.c called\n");
    return 2;
}

编译bar.c和foo.c的:

Compile bar.c and foo.c:

$ gcc -fPIC -c bar.c
$ gcc -fPIC -c foo.c

添加文件bar.o到静态库:

Add bar.o to a static library:

$ ar r libbar.a bar.o

现在使用文件foo.o并链接静态libbar.a创建一个共享库

Now create a shared library using foo.o and link with static libbar.a

$ gcc -shared -o libfoo.so foo.o -L. -lbar

编译main.c中与共享库链接libfoo.so

Compile main.c and link with shared library libfoo.so

$ gcc -o main main.c -L. -lfoo

将LD_LIBRARY_PATH设置为libfoo.so找到并运行主:

Set LD_LIBRARY_PATH to find libfoo.so and run main:

$ 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中栏的版本叫做,不链接到共享库的版本。

Notice that the version of bar in main.c is called, not the version linked into the shared library.

在main2.c我们有:

In main2.c we have:

#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链接):

Compile and run main2.c (notice we dont need to explicitly link with 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中共享库及主要调用栏中的共享库调用栏

Now foo in the shared library calls bar in the shared library and main calls bar in main.c

我不认为这种行为是直观的,它是使用的dlopen / dlsym进行更多的工作,但它确实解决了我的问题。

I don't think this behaviour is intuitive and it is more work to use dlopen/dlsym, but it does resolve my problem.

您的意见再次感谢。

这篇关于一个库的多个版本链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆