質問

I'm probably doing something wrong, being a newbie. Could you please help me out?

I've written a simple Hello World program in C called hello.c, and ran the following command:

gcc -S hello.c

That produced hello.s. Then I used that file with GNU assembler, as:

as hello.s

Which produced a non-executable a.out, which still needs to be linked, I understand?

I try to link it by using ld, like so:

ld a.out

But get the following error:

a.out: file not recognized: File truncated

And ld deletes my file.

This is an x86 Ubuntu system. What am I doing wrong? Many thanks!

役に立ちましたか?

解決

My first question would be: why are you assembling the code? If you want the assembler code the, by all means, use gcc -S to get it (for viewing, I guess).

But you don't need to run that through as to keep going, just use:

gcc -o hello hello.c
gcc -S hello.c

That first step will turn the C source directly into an executable, the second will give you your assembler source.

Your specific problem may be that ld tries to write its output to a.out. If that's also your input file, it may well be being destroyed in the process of running ld. You could try renaming a.out to a.in before running the ld command: ld a.in.

他のヒント

Here is how I do it:

> gcc -S forums.c 
> as forums.s -o forums.o
> gcc forums.o -o forums
> ./forums 
test

Why do I invoke gcc instead of ld? Because GCC takes care of linking the C runtime, and doing other implementation-dependant stuff. If you want to see that, use the --verbose option:

> gcc --verbose forums.o -o forums
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../configure --prefix=/usr --enable-shared --enable-languages=c,c++,fortran,objc,obj-c++ --enable-threads=posix --mandir=/usr/share/man --infodir=/usr/share/info --enable-__cxa_atexit --disable-multilib --libdir=/usr/lib --libexecdir=/usr/lib --enable-clocale=gnu --disable-libstdcxx-pch --with-tune=generic
Thread model: posix
gcc version 4.4.0 (GCC) 
COMPILER_PATH=/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/:/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/:/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'forums' '-mtune=generic'
 /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/collect2 --eh-frame-hdr -m elf_i386 --hash-style=both -dynamic-linker /lib/ld-linux.so.2 -o forums /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/../../../crt1.o /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/../../../crti.o /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/crtbegin.o -L/usr/lib/gcc/i686-pc-linux-gnu/4.4.0 -L/usr/lib/gcc/i686-pc-linux-gnu/4.4.0 -L/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/../../.. forums.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/crtend.o /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/../../../crtn.o

EDIT: okay, I tried this all out on my system, and I think I know what the problem is. ld is writing to a.out (its default output file), while reading from it at the same time. Try something like this:

ld a.out -o myprog

reinstall glibc-devel anyway you can and check if it work. this process work for me.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top