Issue
I have written a Hello World program in C. I used the command gcc -S -o hello.s hello.c
(code was in hello.c) to generate assembly, then used as
to assemble it. Finally I linked the object code using ld
, but it gave me the following error:
ld: a.out:hello.c:(.text+0xa): undefined reference to `__main'
ld: a.out:hello.c:(.text+0x16): undefined reference to `puts'
The C code was as follows:
#include <stdio.h>
int main(void) {
printf("Hello world!\n");
}
I use an Intel Core i3 processor, and used MinGW to compile. My operating system is Windows 7.
This was the generated assembly:
.file "hello.c"
.text
.def ___main; .scl 2; .type 32; .endef
.section .rdata,"dr"
LC0:
.ascii "Hello world!\0"
.text
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
LFB13:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
andl $-16, %esp
subl $16, %esp
call ___main
movl $LC0, (%esp)
call _puts
movl $0, %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE13:
.ident "GCC: (MinGW.org GCC Build-2) 9.2.0"
.def _puts; .scl 2; .type 32; .endef
Solution
Thanks to Nate Eldredge for helping me get the answer.
To link the file produced by as
, use gcc -v (file)
.
Answered By - R Z Answer Checked By - Cary Denson (WPSolving Admin)