2012-06-21 64 views
0

我還在學習C和我明白,要擺脫最隱式聲明的警告,你在開始添加原型頭。但是我對你在代碼中使用外部方法時所做的事感到困惑。函數的隱式聲明?

這是當我使用的方法外

#include <stdio.h> 
    #include <string.h> 
    int main(void) 
    { 
     int arrayCapacity = 10; 
     int maxCmdLength = 20; 

     int A[arrayCapacity]; 
     int count = 0; /* how many ints stored in array A */ 
     char command[maxCmdLength + 1]; 
     int n; 

     while (scanf("%s", command) != EOF) 
     { 

     if (strcmp(command, "insert") == 0) 
     { 
      scanf("%d", &n);     
      insert (n, A, arrayCapacity, &count); 
      printArray(A, arrayCapacity, count); 
     } 
     else if (strcmp(command, "delete") == 0) 
     { 

      scanf("%d", &n); 
      delete(n,A,&count); 
      printArray(A, arrayCapacity, count); 

     }  
     else 
     { 
      scanf("%d", &n); 
      printArray(A, arrayCapacity, count); 
     } 
    } 
    return 0; 
    } 

方法printArray,插入我的代碼,並刪除全部的形式:printArray.o, insert.o, delete.o

這是我編譯了一個程序:gcc -Wall insert.o delete.o printArray.o q1.c 和我得到這些警告:

q1.c: In function âmainâ: 
q1.c:20: warning: implicit declaration of function `insert' 
q1.c:21: warning: implicit declaration of function `printArray' 
q1.c:30: warning: implicit declaration of function `delete' 

我試過,包括這頭,但我得到è沒有找到文件或目錄的錯誤。

任何幫助表示讚賞。

+0

什麼是對的#include是你使用,你在哪裏放頭? – Mark

回答

0

您需要包括正確的頭擺脫這樣的警告。

如果你得到一個「未找到文件」的錯誤,儘量把它們作爲

#include "myheader.h" 

,並把你的頭文件在同一目錄下的源代碼。

一般來說,#include "file"爲程序員定義的報頭,而#include <file>是或標準頭。

2

把它們放在一個頭文件foo.h像這樣:

extern void printArray(int *A, int capacity, int count); 
... 

然後包括該文件在源

#include "foo.h" 
0

你應該能夠只是把函數原型在頂部該文件就像你在同一個文件中爲其他功能做的那樣。鏈接器應該照顧其餘的。

0

你從哪裏得到那些.o文件?如果你自己寫的這些,那麼你應該建立相應的.h文件。如果你從別的地方得到這些文件,那麼你應該尋找在同一個地方的標頭。

0

如果所有的調用函數是在的main()函數編譯器會知道他們的名字,返回類型和參數簽名並全部三種性質,每個下面的函數調用比賽之前寫的。

一些程序員喜歡先寫一個函數簽名,並做在以後的執行時間。

函數聲明唯一必要的時候是使用協程序:functionA調用函數B,然後調用functionA。

完成如下:

type a(...signatureOfA...) 

/* compiler now knows about a() */ 

type b(...signatureOfB...) 
{… 
// implementation of b 
a(…arguments for a…); 

/* compiler knows about above */ 
…} 

type a(...signatureOfA...)i 
{… 
// implementation of a 
b(…arguments for b…); 

/* compiler knows about above */ 
…} 

int main() 
{ 
a(… arguments for a…); 
/* compiler knows */ 
return(code); 
}