linux linking assembly with gcc gives many errors-Collection of common programming errors
I am trying to compile and link a simple “hello, world!” program with GCC. This program uses the “printf” C function. The problem that I am having is that the terminal throws back multiple errors. I am running Archlinux, compiling with NASM, linking with GCC. Here is my code:
; ----------------------------------------------------------------------------
; helloworld.asm
;
; Compile: nasm -f elf32 helloworld.asm
; Link: gcc helloworld.o
; ----------------------------------------------------------------------------
SECTION .data
message db "Hello, World",0
SECTION .text
global main
extern printf
section .text
_main:
push message
call printf
add esp, 4
ret
The errors that I receive are as follows:
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/libgcc.a when searching for -lgcc
/usr/bin/ld: cannot find -lgcc
collect2: error: ld returned 1 exit status
Can someone tell me what is causing these errors and what I need to do to fix them?
Thanks in advance,
RileyH
-
For such things, you should first understand what exactly
gcc
is doing. So usegcc -v helloworld.o -o helloworld
and what is happenning is that you have a 64 bits Linux and linking a 32 bits object in it. So try with
gcc -m32 -v helloworld.o -o helloworld
But I think that you should avoid coding assembly today (optimizing compilers do a better work than you can reasonably do). If you absolutely need a few assembly instructions, put some
asm
in your C code.BTW, you could compile with
gcc -fverbose-asm -O -wall -S helloworld.c
and look inside the generatedhelloworld.s
; and you could also pass.s
files togcc
Originally posted 2013-11-09 23:31:51.