Saturday, 15 August 2015

gcc - Is function declaration essential to C programming? -



gcc - Is function declaration essential to C programming? -

i used believe should declare function defined in file before utilize it, changed way of thinking due experience of programming. 3 files, c , asm:

main.c

extern test_str; /*extern myprint*/ --> if add together line, gcc study error: called object ‘myprint’ not function void print_string() { myprint("a simple test", test_str); }

kernel.asm

extern print_string [section .text] global _start global test_str test_str dd 14 _start: phone call print_string jmp $

another.asm

[section .text] global myprint myprint: mov edx, [esp + 8] mov ecx, [esp + 4] mov ebx, 1 mov eax, 4 int 0x80 ret

compile

nasm -f elf another.asm -o another.o gcc -c -g main.c -o main.o nasm -f elf kernel.asm -o kernel.o ld -o final main.o kernel.o another.o

result

./final simple test

in view, if want utilize function myprint in main.c, should declare using extern beforehand, because myprint defined in file, result opposite. main.c shows above. if add together line extern myprint, error. however, without declaration, right result. what's more, didn't define function myprint in main.c, why can utilize function? shouldn't declare beforehand?

when phone call function without prototype compiler makes assumptions , guesses parameters of function. should declare it, declare function:

void myprint(const char *, const char *); /* or whatever. */

c gcc assembly compilation nasm

No comments:

Post a Comment