title: Can't link shared library with -mx32 and gcc 4.7 or gcc 4.8
body: <p>I'm trying to compile a large codebase written for a 32 bit embedded processor to run on a 64 bit desktop processor for simulation/unit testing.  I need the resulting object to be a shared library.  This is not an issue in Windows, I can build a dll like this (/DWIN32) and it runs fine.</p>
  
  <p>In Linux, I'm able to compile and link with the -m32 option given to both gcc and the linker and get a shared library out.  The problem is, this library is (just like I specified with -m32) a 32 bit library and will not run on my 64 bit arch.  With Python, I try to load the library (using ctypes.cdll.LoadLibrary())</p>
  
  <pre><code>OSError: out.so: wrong ELF class: ELFCLASS32
  </code></pre>
  
  <p>I discovered the -mx32 option, and according to the <a href="https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html#x86-Options" rel="nofollow">docs</a> this is exactly what I want:</p>
  
  <blockquote>
    <p>The -mx32 option sets int, long, and pointer types to 32 bits, and
    generates code for the x86-64 architecture.</p>
  </blockquote>
  
  <p>So, I pass -mx32 to the compiler and the linker (replacing my -m32 option) and get the following (snipped the output):</p>
  
  <pre><code>/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.so when searching for -lstdc++
  /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a when searching for -lstdc++
  /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.8/32/libstdc++.so when searching for -lstdc++
  /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.8/32/libstdc++.a when searching for -lstdc++
  /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.so when searching for -lstdc++
  /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a when searching for -lstdc++
  /usr/bin/ld: cannot find -lstdc++
  /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/libm.so when searching for -lm
  /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/libm.a when searching for -lm
  /usr/bin/ld: skipping incompatible /usr/lib/x86_64-linux-gnu/libm.so when searching for -lm
  /usr/bin/ld: skipping incompatible /usr/lib/x86_64-linux-gnu/libm.a when searching for -lm
  /usr/bin/ld: skipping incompatible /usr/lib/x86_64-linux-gnu/libm.so when searching for -lm
  /usr/bin/ld: skipping incompatible /usr/lib/x86_64-linux-gnu/libm.a when searching for -lm
  /usr/bin/ld: cannot find -lm
  </code></pre>
  
  <p>I get the same results with gcc 4.7 and gcc 4.8.  The above output is from gcc 4.8.  </p>
  
  <p>I have gcc-4.8-multilib and g++-4.8-multilib installed.</p>
  
  <p>My library path is:</p>
  
  <pre><code>LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:/usr/lib/gcc/x86_64-linux-gnu/4.8/32:/usr/lib/gcc/x86_64-linux-gnu/4.8/
  </code></pre>
  
  <p>From .bashrc I've specified it like this (below), in despair adding the /4.8/ and /4.8/32/ stuff after reading that the linker would only bind with the libraries that work.  </p>
  
  <pre><code>export LIBRARY_PATH=/usr/lib/$(gcc -print-multiarch):/usr/lib/gcc/x86_64-linux-gnu/4.8/32:/usr/lib/gcc/x86_64-linux-gnu/4.8/
  </code></pre>
  
  <p>As I mention, this works quite well already on Windows as a dll, I have to believe I'm just missing something.  Pointers should be 32 bits and longs should be 32 bits, the whole thing should run on x86_64.  -mx32 says it will do exactly that (right?). </p>
  
  <p>Checking one of the objects after compiling in -m32:</p>
  
  <pre><code>$:~/project$ file foo.o
  foo.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
  </code></pre>
  
  <p>And checking the same object after compiling with -mx32:</p>
  
  <pre><code>$:~/project$ file foo.o
  foo.o: ELF 32-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
  </code></pre>
  
  <p>Am I going about this the wrong way?  Can I still use my 32-bit shared library with some sort of compatibility layer?</p>
  
  <p>I saw a <a href="https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=667025" rel="nofollow">bug report</a> against gcc 4.7 regarding these link errors...  But I didn't see much conclusion from it.  <a href="https://sites.google.com/site/x32abi/" rel="nofollow">This page</a> says that gcc 4.8 is the recommended minimum for x32, so I installed it.  Still -- I can't get it to link.  -m32 links fine.</p>