D
D
DVoropaev2019-06-04 12:42:46
linux
DVoropaev, 2019-06-04 12:42:46

Why can't I compile GNU assembler?

the goal is to see the process of turning C code into an executable file

#include <stdio.h>

int main(){
   printf("Hello");
   return 0;
}
Normal compilation:
$gcc ./main.c -o main.o
$ ./main.o 
Hello

.c -> assembler -.> executable file
$gcc -S ./main.c -o ./main.s     #превращаем в ассемблерный код
$ as ./main.s -o ./main.o  #ассемблируем
$ ld ./main.o #пытаемся линковать, но выскакивает ошибка
ld: warning: cannot find entry symbol _start; defaulting to 00000000004000b0
./main.o: In function `main':
main.c:(.text+0xf): undefined reference to `printf'

#А если пытаемся линковать средствами gcc, то все работает
$ gcc ./main.o
$ ./a.out 
Hello

Assembly code just in case
.file	"main.c"
  .section	.rodata
.LC0:
  .string	"Hello"
  .text
  .globl	main
  .type	main, @function
main:
.LFB0:
  .cfi_startproc
  pushq	%rbp
  .cfi_def_cfa_offset 16
  .cfi_offset 6, -16
  movq	%rsp, %rbp
  .cfi_def_cfa_register 6
  movl	$.LC0, %edi
  movl	$0, %eax
  call	printf
  movl	$0, %eax
  popq	%rbp
  .cfi_def_cfa 7, 8
  ret
  .cfi_endproc
.LFE0:
  .size	main, .-main
  .ident	"GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609"
  .section	.note.GNU-stack,"",@progbits

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-06-04
@DVoropaev

Because when gcc runs ld, it passes a lot more options to it, including the required libraries, not just the program's object module.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question