LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   compiling 32 bit application on 64 bit linux(x86_64) (http://www.linuxquestions.org/questions/linux-software-2/compiling-32-bit-application-on-64-bit-linux-x86_64-a-606548/)

uttam_h 12-13-2007 11:02 AM

compiling 32 bit application on 64 bit linux(x86_64)
 
hi all,
i have a 64 bit linux machine.

$uname -a
Linux SVRDELLD30 2.6.9-42.ELsmp #1 SMP Tue Aug 15 10:35:26 BST 2006 x86_64 x86_64 x86_64 GNU/Linux

here by default gcc creates 64 bit executable. but for some reason i want to create 32bit executable.

first i want to create 32 bit object files(.o files) and then i want to link those object files.

Please suggest me suitable gcc options.

Thanks and Regards,
uttam hoode

weibullguy 12-13-2007 11:47 AM

If you have a toolchain that supports multilib, then you would pass the -m32 flag to the compiler to generate 32-bit, i386 compatible code. If you don't have a toolchain with multilib support, you can't create 32-bit apps or libs on an x86_64 system.

uttam_h 12-13-2007 11:58 AM

$gcc -print-multi-lib
.;
32;@m32


$gcc -print-multi-os-directory -m32; gcc -print-multi-os-directory -m64; gcc -print-multi-os-directory
../lib
../lib64
../lib64

when i gave -m32 option gcc created 32 bit object files....

$file Logger.o
Logger.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped

but im getting error while linking .o files.

Thanks and Regards,
uttam hoode

David1357 12-13-2007 01:28 PM

Quote:

Originally Posted by uttam_h (Post 2989512)
but im getting error while linking .o files.

Try passing "-m32" to the linker command.

weibullguy 12-13-2007 03:51 PM

Quote:

Originally Posted by uttam_h (Post 2989512)
but im getting error while linking .o files.

The actual error might be more helpful.

osor 12-13-2007 06:11 PM

Quote:

Originally Posted by David1357 (Post 2989605)
Try passing "-m32" to the linker command.

If you are using GNU ld, and it complains about incompatible objects, try something like this instead:
Code:

ld -m elf_i386 -o foo foo.o

uttam_h 12-13-2007 10:51 PM

hi weibullguy,
im getting following errors while linking(using gcc)


/usr/bin/ld: warning: i386 architecture of input file `global.o' is incompatible with i386 output
/usr/bin/ld: warning: i386 architecture of input file `Logger.o' is incompatible with i386 output
/usr/bin/ld: warning: i386 architecture of input file `tap311.o' is incompatible with i386 output

when i used ld with -m elf_i386 (as suggest by osor) i got follwing errors but executable is created

ld: warning: cannot find entry symbol _start; defaulting to 0000000008048a80


and executable is throwing this error


-bash: ./myapp: /usr/lib/libc.so.1: bad ELF interpreter: No such file or directory


Thanks and Regards,
uttam hoode

David1357 12-14-2007 08:47 AM

Quote:

Originally Posted by uttam_h (Post 2990094)
when i used ld with -m elf_i386 (as suggest by osor) i got follwing errors but executable is created

ld: warning: cannot find entry symbol _start; defaulting to 0000000008048a80

Try adding "-lcrt0" to make sure you are getting the C startup code.

osor 12-14-2007 11:09 AM

An alternative to all these shenanigans is to use gcc as your linker frontend. It will automatically pick the correct options for you:
Code:

gcc -m32 -o output tap311.o Logger.o global.o

uttam_h 12-16-2007 08:08 AM

hi all,
created object files and i am able to link them using -m32....now i have a 32bit executable on x86_64 machine...but when i tried to execute i am getting segmentation fault error.

(i copied 32bit executable from i386 machine to x86_64 machine and it s working fine. both i386 and x86_64 machines have same version of gcc and glibc)

Thanks and Regards,
uttam hoode

mhtrinh 12-21-2007 08:11 AM

Hi

I have the same problem :
I have a Xeon 64bit processor, Fedora 8 x86_64 :
Quote:

Linux sbtn155**** 2.6.23.1-49.fc8 #1 SMP Thu Nov 8 22:14:09 EST 2007 x86_64 x86_64 x86_64 GNU/Linux
And another machine with a 32bit processor, with Fedora core 5 :
Quote:

Linux sbtn151**** 2.6.15-1.2054_FC5 #1 Tue Mar 14 15:48:33 EST 2006 i686 i686 i386 GNU/Linux
I have a simple C code: main.c:
Code:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    printf("Hello world\n");
    return 1;
}

When I compile this on my 32bit machine, the executable run on both machine.

When I compile this on my 64bit machine with the option -m32:
Quote:

gcc -m32 main.c
It run only on the 64bit machine. On the other on, I got this error :
Quote:

$ ./a.out
Floating exception
With google, it seems that am the only human with this problem (X_X)

Here my gcc version :
64bit :
Quote:

$ gcc --version
gcc (GCC) 4.1.2 20070925 (Red Hat 4.1.2-33)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
32bit :
Quote:

$ gcc --version
gcc (GCC) 4.1.0 20060304 (Red Hat 4.1.0-3)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

osor 01-01-2008 02:23 PM

Quote:

Originally Posted by mhtrinh (Post 2998012)
When I compile this on my 32bit machine, the executable run on both machine.

When I compile this on my 64bit machine with the option -m32:


It run only on the 64bit machine. On the other on, I got this error :


With google, it seems that am the only human with this problem (X_X)

Have you considered that the architecture is being used during compilation and/or linking? I.e., there may be architecture-specific optimizations available to your Xeon over your 32-bit machine apart from its 64-bittedness.

On your 64-bit machine, try something like this:
Code:

gcc -m32 -march=i686 main.c

mhtrinh 01-07-2008 03:15 AM

Sorry for the late reply.

I tried the option -march=i686 : nothing changed ... :( (I tried also i386 i586)

Quote:

Have you considered that the architecture is being used during compilation and/or linking? I.e., there may be architecture-specific optimizations available to your Xeon over your 32-bit machine apart from its 64-bittedness.
... I don't know ...

samitpmc 07-17-2008 08:51 AM

Hello

My problem is just opposite. I want to build x64 application from my 32 bit ubuntu machine with gcc 4.1.2 installed.

I have test.cpp

#include <iostream>

int main()
{
cout<<"hello world"
return 0;
}

now compiling this code using
g++ -m64 test.cppI get error.

/usr/bin/ld: skipping incompatible /usr/lib/gcc/i486-linuxgnu/4.1.2/../../../libm.so when searched for lm

/usr/bin/ld: cannot find lm.

waiting for your suggestion.

Regards
Amit

resetreset 07-19-2008 04:28 AM

Quote:

Originally Posted by uttam_h (Post 2992146)
hi all,
created object files and i am able to link them using -m32....now i have a 32bit executable on x86_64 machine...but when i tried to execute i am getting segmentation fault error.

(i copied 32bit executable from i386 machine to x86_64 machine and it s working fine. both i386 and x86_64 machines have same version of gcc and glibc)

Thanks and Regards,
uttam hoode

YOU CANT RUN A 32 BIT EXE ON A 64BIT MACHINE!!! WHAT KIND OF PERSON KNOWS THE TERMS "32-BIT", "64-BIT", PROGRAMS, BUT DOESNT KNOW THAT??!!


All times are GMT -5. The time now is 08:50 PM.