Note that there are some explanatory texts on larger screens.

plurals
  1. POIs function declaration essential to C programming?
    primarykey
    data
    text
    <p>I used to believe that we should declare a function which is defined in another file before use it, but recently I changed my way of thinking due to an experience of programming. For three files, <code>C</code> and <code>ASM</code>:</p> <p><em><strong>main.c</em></strong></p> <pre><code>extern test_str; /*extern myprint*/ --&gt; If I add the line, gcc will report an error: called object ‘myprint’ is not a function void Print_String() { myprint("a simple test", test_str); } </code></pre> <p><em><strong>kernel.asm</em></strong></p> <pre><code>extern Print_String [section .text] global _start global test_str test_str dd 14 _start: call Print_String jmp $ </code></pre> <p><em><strong>another.asm</em></strong></p> <pre><code>[section .text] global myprint myprint: mov edx, [esp + 8] mov ecx, [esp + 4] mov ebx, 1 mov eax, 4 int 0x80 ret </code></pre> <p><em>compile</em></p> <pre><code>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 </code></pre> <p><em>result</em></p> <pre><code>./final a simple test </code></pre> <p>In my view, if I want to use the function <code>myprint</code> in <strong><em>main.c</em></strong>, I should declare it using <code>extern</code> beforehand, because <code>myprint</code> is defined in another file, but the result is exactly opposite. Just as <strong><em>main.c</em></strong> shows above. If I add the line <code>extern myprint</code>, I will get an error. However, without that declaration, I will get the right result. What's more, I didn't define function <code>myprint</code> in <strong><em>main.c</em></strong>, why can I use that function? Shouldn't I declare it beforehand?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload