【问题标题】:Use 32bit shared library from 64bit application?使用来自 64 位应用程序的 32 位共享库?
【发布时间】:2012-04-06 05:12:56
【问题描述】:

我为我的渲染包装器创建了一个简单的 linux 32 位共享库 (.so),但是当我发现我只能 通过 32 位应用程序使用它们............

这就是我的代码的样子:

RendIFace.h:

//Basic renderer interface
struct Renderer
{
    int type;
    ...other things
};

GLRend.c:

#include "RendIFace.h"
struct Renderer* GLRendererCreate(int width,int height,int bytesPerPixel)
{
    struct Renderer* rend = (struct Renderer*)malloc(sizeof(Renderer));

    rend->type = GLR;
    ..other things

    return rend;
}

SDLRend.c:

#include "RendIFace.h"
struct Renderer* SDLRendererCreate(int width,int height,int bytesPerPixel)
{
    struct Renderer* rend = (struct Renderer*)malloc(sizeof(Renderer));

    rend->type = SDLR;
    ..other things

    return rend;
}

我将它们编译为共享的 32 位库 (.so) 并通过主应用程序加载它们...

但是现在有一个大问题。我的库都是 32 位并返回 32 位指针,这意味着我无法通过 一个 64 位应用程序,无需重建所有库代码库(!!!)。

所以我想问问有经验的人:我如何处理这个问题?两种架构都可以只使用一个共享库吗???

【问题讨论】:

  • 我认为这行不通。 DLL/SO 被加载 程序进程。当位数不同时,我从未见过/听说过它可以工作(因为 32 位 IA32 现在正试图在 64 位 AMD64 上下文中运行,除此之外,CC 可能不同)。
  • 您也在编写应用程序吗?还是只是库?
  • 我正在编写应用程序和库...

标签: c linux gcc shared-libraries


【解决方案1】:

您必须始终如一。 64 位应用程序只能使用 64 位库,而 32 位应用程序只能使用 32 位库。两者都有效;任何一种选择都可以,并且可以为两个系统编译相同的代码。

如果您选择“全 32 位”,请使用:

  • gcc -m32

如果您选择“全 64 位”,请使用:

  • gcc -m64

有时,我会告诉make,C 编译器是gcc -m32(或-m64)而不仅仅是gcc,以确保在任何地方都使用正确的值。

【讨论】:

  • 谢谢。我想我应该切换到静态库。
  • 无论您使用静态库还是共享库,您仍然必须保持一致,将它们全部构建为 32 位或全部 64 位。您不能在单个可执行文件中混合 32 位和 64 位代码。
【解决方案2】:

你不能按照你的要求去做。您必须为同一架构编译最终可执行文件和任何库(静态库和共享库)。

在 GCC 上,这可以通过直接在命令行中传递命令行参数 -m32 或在您的 Makefile 中添加 CCFLAGS 来轻松完成。

虽然可以在 x86_64 操作系统上运行 x86 代码(您只需要拥有所有正确的库及其各自的递归依赖项),但您不能在一个可执行文件或一个地址空间中组合 x86 和 x86_64 二进制文件.

【讨论】:

  • 我认为你没有抓住重点。甚至静态库也被编译为 64 位或 32 位。
猜你喜欢
  • 2023-03-07
  • 2021-12-22
  • 1970-01-01
  • 2014-08-23
  • 2013-11-14
  • 2012-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多