stackuser
stackuser

Reputation: 4191

Linker error when compiling assembly

I am trying to generate assembly language listing for a simple hello world program

I ran gcc -S file.c

and generated the file.s.

I am using the below script using the .s file generated:

#!/bin/bash -x

NAME=file1

for i in *.s; do
        as -o ${i%.s}.o $i
done
ld -static -o $NAME *.o
objdump -D $NAME

When i use this , I get an error:

In function `main':
(.text+0x16): undefined reference to `printf'

Any help would be greatly appreciated.

Upvotes: 0

Views: 995

Answers (1)

compile with gcc -fverbose-asm -S -Wall (and perhaps also -O or -O2)

The -fverbose-asm generates some helpful comments in the assembler.

Use gcc -v to understand how is the program linked. You need startup code (like some crt0.o) and you need to link the libc.so.6 or libc.a at least.

When compiling and linking with gcc -v the intermediate steps, and the implicit object files and libraries, are shown. gcc is just starting other programs like cc1 (the compiler proper) and as (assembler) and ld linker. Sometimes collect2 is a sort of linker.

BTW, you could write some assembly code and use it thru gcc. For instance, code just one routine in assembly, and the rest in C. Then run gcc -v -Wall routine.s main.c -o prog

Read also the Linux Assembly HowTo.

Upvotes: 1

Related Questions