LinuxQuestions.org

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

uttam_h 12-13-2007 10: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 10: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 10: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 12: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 02: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 05: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 09: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 07: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 10: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 07: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 07: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 01: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 02: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 07: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 03: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??!!

johnsfine 07-19-2008 06:02 AM

Quote:

Originally Posted by samitpmc (Post 3217560)
My problem is just opposite. I want to build x64 application from my 32 bit ubuntu machine with gcc 4.1.2 installed.

You need 64-bit .so files and/or .a files (and probably at least one startup .o file) for linking and you need g++ to be able to find them. From your symptom description below, I would guess you haven't even loaded those files. Once you have the files, I'm not sure what you need to set up so that g++ will find them.

Maybe some expert will answer (I'm definitely not). But meanwhile, here is the way I would attack the problem if I needed to do what you're trying:

Quote:

/usr/bin/ld: skipping incompatible /usr/lib/gcc/i486-linuxgnu/4.1.2/../../../libm.so when searched for lm
Identify the problem files one at a time. You just identified the first (libm.so). As you fix each, the linker will identify more.

Check with your package manager to find out which 32-bit package contained that file.

Go to the ftp site for your distribution and download (not install) the 64-bit build of that same package.

Open the package with ark (instead of the package manager) to extract the .gz file that contains the package data.

Open that .gz file and see what directories contain .so, .a or .o files. Make "64" versions of those same directories (for example, you will find libm.so goes in /usr/lib, so make a corresponding /usr/lib64 directory).

Drag each of those files from the .gz to the "64" directory you created for it.

Figure out the right g++ switches or environment variables to make it look there.

Retry the build.

I'm sure experts know a cleaner way to automate some of the above steps. But the above is the way I know.

Quote:

Originally Posted by resetreset (Post 3219692)
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??!!

I tried your profile and the link to your other posts to see if you routinely post things like the above. It didn't look that way. So what's the problem? You replied to an old post (in a thread someone reopened with a significant change of question). Your reply is totally incorrect as well as missing the point of even the old post you quoted. With the all caps and "what kind of person" one would assume you're just trying to provoke a fight. Probably I misunderstood what I saw in your other posts and you are just trying to provoke a fight. Please don't. Please allow this thread to provide useful answers to those looking for help.

knudfl 07-19-2008 02:33 PM

To samitpmc # 14 : lm ~ libm.so ... please see this
http://www.linuxquestions.org/questi...mpiler-656359/

Regards


All times are GMT -5. The time now is 10:56 AM.