C angular housing and trap

I wonder why this works?

short main [] ={};

This is the only content in the file. It compiles correctly on gcc. But when I run, it prints a segmentation error. When I rename main, the compiler gives errors. Can someone explain to me what is happening here.

+3
source share
3 answers

Apparently, the linker does not know the type of global objects (for example: a variable or function), but only the address; therefore, it binds the program as if your variable were a function. What happens for obvious reasons.

+4
source

Are you getting an error?

Undefined symbols:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

This is not a compiler error, but a linker error.

.

, int main(), , main() (, ),

short main[] = {};

( short main ) , .

, int main(), . . main, , , . , C ABI . , main , "-, main " , .

, .

, main . , (, ). , - ( SEGFAULT).

-Wall gcc, :

<stdin>:1: warning: ‘main’ is usually a function
+3

Try compiling with lots of options. :)

For example, adding a simple -wall

gcc -Wall test.c -o t
test.c:1: warning: ‘mainis usually a function

I have not read the corresponding standard page, but apparently you just have to have some basic way to compile, not necessarily a function ...

+1
source

Source: https://habr.com/ru/post/1762657/


All Articles