开发者

A C program compiled under 32-bit Debian Squeeze causes a segfault on my friend's 64-bit one

开发者 https://www.devze.com 2023-01-20 21:55 出处:网络
Not so long ago I\'ve installed Debian and configured it with my friend\'s help. Yesterday I have downloaded GCC 4.4 and I created a simple program to test it out.

Not so long ago I've installed Debian and configured it with my friend's help.

Yesterday I have downloaded GCC 4.4 and I created a simple program to test it out.

This is the code:

#include <stdio.h>

int main () {
    int result;
    printf ("Hello Wor... Linux! This is my %dst program compiled in Debian.\nHow many is 2+2?\n", 1);
    scanf ("%d", &result);
    while (result!=4) {
        printf ("Oh no! You're not going anywhere until you type the correct result! 2+2 is?\n");
        scanf ("%d", &result);
    }
    printf ("Congrats!\n");
    return 0;
}

I've compiled it by typing gcc-4.4 myfile.c in bash. Then I've tried to run the resulting binary file and it worked just as I wanted it to. Then I've sent the binary file to my friend to test it on his PC also. When he tried to run it, he received a segmentation fault message and the program didn't work.

He also uses Debian and his kernel's version is very similar to mine (2.6.32-5-686). The only difference is that his kernel is an amd-64 one (he owns a 64-bit proces开发者_运维百科sor, while mine is 32-bit).

Why is this happening? Does it mean that 64-bit Linux users will be unable to run my 32-bit programs? If so, can I compile it in a way which will let them to run it?

Please note that I'm not really experienced with Linux.


he may need a chroot for it.

apt-get install ia32-libs 

should work for most cases.

see "Using an IA32 chroot to run 32bit applications" http://alioth.debian.org/docman/view.php/30192/21/debian-amd64-howto.html#id292205


Alternatively, set up your compiler to target 64-bit binaries by following the instructions at the OSDev wiki: In brief:
Set up the new repos in /etc/apt/sources.list

    deb http://www.tucs.org.au/~jscott4/debian/ stable main    #Primary Mirror. Hosted by University of Tasmania.

Add the signing key:

    gpg --recv-keys 0x2F90DE4A
    gpg -a --export 0x2F90DE4A | sudo apt-key add -

Update your repo indices and get the appropriate cross-compilation package:

    apt-get update
    apt-get install osdev-crosscompiler-x86-64-elf

Then use the x86_64-elf variant of gcc to target x64. For instance

    x86_64-elf-gcc --pedantic -Wall -o foo foo.c

(In fact all the GCC tools and Binutils will have an x86_64-elf- variant now.)

EDIT -- Vastly improved instructions by pulling from a reference instead of from memory. EDIT -- removed stale mirror


chroot is one option. But remember it requires a lot of disk space as it installs 32-bit libraries.

Alternatively you can compile your file for a 64-bit environment by using the -m64 compiler flag of gcc which sets int to 32 bits and long and pointer to 64 bits and generates code for AMD's x86-64 architecture.

0

精彩评论

暂无评论...
验证码 换一张
取 消