Use 32bit shared library from 64bit application?

11,035

Solution 1

You must be consistent. A 64-bit application can only use 64-bit libraries and a 32-bit application can only use 32-bit libraries. Both work; either choice is fine, and it's possible to compile the same code for both systems.

If you go for 'all 32-bit', use:

  • gcc -m32

If you go for 'all 64-bit', use:

  • gcc -m64

Sometimes, I'll tell make that the C compiler is gcc -m32 (or -m64) rather than just gcc to ensure the right value is used everywhere.

Solution 2

You can't do what you're asking. You must compile both the final executable and any libraries (both static and shared) for the same architecture.

On GCC, this can be done easily by passing the command line argument -m32 either directly in the command line or by adding it CCFLAGS in your Makefile.

While it is possible to run x86 code on a x86_64 operating system (you just need to have all the right libraries and their respective recursive dependencies), you cannot, in one executable or in one address space, combine x86 and x86_64 binaries.

Share:
11,035
user1010005
Author by

user1010005

Updated on June 08, 2022

Comments

  • user1010005
    user1010005 almost 2 years

    I have created a simple linux 32bit shared library(.so) for my rendering wrappers but i've hit a wall when i figured that i can only use them through 32bit applications....................

    This is how my code looks like:

    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;
    }
    

    And i compile both as shared 32bit libraries(.so) and load them through the main application...

    But now there is a big problem.My libraries are all 32bit and return 32bit pointers which means that i can't use them through an 64bit application without rebuilding all the library code base(!!!).

    So i would like to ask more experienced people : How do i handle this issue ? Is it possible to use just a single shared library for both architectures ???

    • Admin
      Admin about 12 years
      I don't think this is going to work. A DLL/SO is loaded into the programs process. I've never seen/heard of it work when the bitness differs (as the 32-bit IA32 is now trying to run in a 64-bit AMD64 context and, among other things, the CC likely differs).
    • Java42
      Java42 about 12 years
      Are you also writting the application? or just the lib?
    • user1010005
      user1010005 about 12 years
      Im writing both the app and lib...
  • user1010005
    user1010005 about 12 years
    Thank you.I guess i should switch to static libraries then.
  • harry
    harry about 12 years
    I think you're missing the point. Even static libraries are compiled for either 64 or 32 bit.
  • Jonathan Leffler
    Jonathan Leffler about 12 years
    Whether you use static or shared libraries, you will still have to be consistent and build them either all 32-bit or all 64-bit. You can't mix 32-bit and 64-bit code in a single executable.