views:

165

answers:

4

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 processor, 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.

A: 
  1. Never use scanf() for user input. It has no delimiters, so somone can break your program.
  2. Do not use the int type, because its size is not machine independent defined.
Peter Miehle
Doesn't really answer the question
Andrew Cooper
1.I know, I've broken it myself by typing a `float` number instead of an `int` one. :) Anyway, what other way of receiving the input do you recommend? I don't have a big experience in C, because I was creating C++ programs all the time. 2.Should I then specify the int's length, by typing `long int` or `short int` instead of just `int`?
rhino
1. `scanf()` is fine in the appropriate context. This use is appropriate. 2. `int` is defined to be a minimum of 16 bits wide, so is fine to use here.
Robie Basak
+4  A: 

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

Orbit
Thanks for your useful answer. I cannot try your solution at the moment, but I believe it'll work (it seems so after reading the webpage), that's why I've accepted your answer.
rhino
Since package size came up below... The ia32-libs installed package size is about 73 MB.
Eric Towers
A: 

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.

codaddict
This didn't work for me. I've tried it by typing `gcc-4.4 -m64 myfile.c` and I've received the following error: `In file included from /usr/include/features.h:378, from /usr/include/stdio.h:28, from myfile.c:1: /usr/include/gnu/stubs.h:9:27: error: gnu/stubs-64.h: No such file or directory`
rhino
+1  A: 

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

Eric Towers
Thanks for the answer, I'm going to test it soon. By the way, can you explain what does `-fPIC` do? EDIT: And I forgot an important thing. Is the multilib version big? Well, I have my Debian on a pendrive, so...
rhino
The apt-get will probably need a bit more than 10 MB for the install. Normally you get a hint about how much space it needs first. The -fPIC is recent habit. Edited out.
Eric Towers
I'm sorry, but this doesn't work for me. I've tried exactly what you said, but the resulting binary file still generates a segfault on my friend's PC.
rhino
@rhino: Significantly updated, including better information than that provided by my memory.
Eric Towers
Thank you, I'll try it tomorrow and say if it works. If it will, I'll accept your answer. For now, I've upvoted it.
rhino
Unfortunately, I couldn't even install it. When I typed `apt-get update`, a following warning has appeared: "Could not download http://www.pedigree-project.org/debian/dists/stable/main/binary-i386/Packages.gz - 404 Not Found". I've tried to install `osdev-crosscompiler-x86-64-elf` though, but it didn't because of the missing library (exactly `libmpfr1ldbl`).
rhino
@rhino: It does appear that the one mirror is stale, so I've removed it from the above. The lib, however, should have automagically installed. It's available from the normal lenny and sid repos. ( http://packages.debian.org/lenny/libmpfr1ldbl , http://packages.debian.org/sid/libmpfr1ldbl ) Any more hints about what was wrong with the lib? (Maybe requires a newer version than 2.3.1 on lenny or 2.4.2 on sid?)
Eric Towers
Maybe that's the reason, there's no Squeeze version there, only Lenny and Sid ones. And I use Squeeze.
rhino
@rhino: Could try "-m" with apt-get (and cross your fingers). <code> apt-get -m install osdev-crosscompiler-x86-64-elf</code>. This should try to ignore missing packages. ... Then ... don't use long doubles in your code. Alternatively, we can install the sid amd64 variant. (See http://packages.debian.org/sid/amd64/libmpfr1ldbl/download , then <code>dpkg -i _filename_</code> )
Eric Towers
Unfortunately `-m` also didn't work, the result was the same as before, apt-get yelled about `libmpfr1ldbl` again. I'll check the Sid's amd64 variant later.
rhino