如何编写C .so库以替补多现有的C ++ .so库?

如何编写C .so库以替补多现有的C ++ .so库?

问题描述:

让我解释的情况。我们有一个传统的C ++编译.so库。这个库中的函数声明与的externC{} ,所以该库可通过C和C ++程序,再加上,由于某种原因,它与创建使用 - 静电libgcc中选项。

Let me explain the scenario. We have a legacy C++ compiled .so library. The functions in this library are declared with extern "c" {}, so the library can be used by both C and C++ program, plus, for some reason it was created with--static-libgcc option.

这老图书馆是很旧,难以维护。现在我们haved设法写它的subsitution,但在C语言。比方说,老图书馆被称为libfoo.so(旧),以及新的一个是libfoo.so(新)。对于给定的文件bar.o,它可与任一新的或旧libfoo.so以创建可执行文件,比方说,bar.exe挂钩。但bar.exe只能与它之前的联系,由换句话说,同样的.so库运行,这两个库不是相互可交换。

This old library is very old and hard to maintain. Now we haved managed to write a subsitution of it, but in C language. Let's say the old library is called libfoo.so(old), and new one is libfoo.so(new). For a given bar.o, it can be linked with either old or new libfoo.so to create a executable, say, bar.exe. But the bar.exe can only run with the same .so library it linked before, by the other words, these two libraries are not mutual exchangable.

编辑#1 :我做了libfoo.so命名的符号链接指向libfoo.so的(旧)或libfoo.so(新)。这个符号链接libfoo.so是LD_LIBRARY_PATH在运行时。

EDIT#1: I made a symbolic link named libfoo.so to point to libfoo.so(old) or libfoo.so(new). This symbolic link libfoo.so is in LD_LIBRARY_PATH at runtime.

编辑#2 :当我挂文件bar.o老libfoo.so和产生bar.exe,如果我运行此bar.exe新libfoo.so,该公司报告的误差未定义的符号。通过纳米这两个libfoo.so,我可以找出旧的这些符号,但不是新的。该符号类似于 _ZSt4cerr ,这是一个C ++的lib重整名称(我虽然它被带到由 - 静电libgcc中),当然还有新的libfoo.so不包含这些类型的符号。

EDIT#2: When I linked bar.o with old libfoo.so and generated bar.exe, if I run this bar.exe with new libfoo.so, it reported error of undefined symbols. By nm these two libfoo.so, I can find out these symbols in old one, but not in new one. The symbols are something like _ZSt4cerr, which is a C++ lib mangled name(I though it was brought by --static-libgcc), and of course the new libfoo.so does not contains those kind of symbols.

编辑#3 :如果我只是编译使用g ++,而不是gcc的,它使任何意义

EDIT#3: If I just compile and link the C code with g++ instead of gcc, does it make any sense?

我应该如何实现呢?

编辑#4 :今天我设法编译/链接新的C程序性相克libfoo的++(静态libgcc的,静态的libstdc ++),这可能会导致被包含在libfoo的所有的C ++符号。所以。这可以使一切顺利运行,但不是我真正想要的。

EDIT#4: Today I managed to compile/link the new C programed libfoo with g++ (with static libgcc, static libstdc++), this can lead to all c++ symbols to be contained in libfoo.so. This can make everything run smoothly, but not what I really want.

如果您创建和使用新的一个环节,你可以得到它与旧的链接?这听起来像你生成的二进制兼容的库,但只在一个方向。

If you build and link with the new one, can you get it to link with the old one? It sounds like you generated a binary compatible library, but only in one direction.