当前位置: 动力学知识库 > 问答 > 编程问答 >

c - implicit declaration of functions?

问题描述:

I'm still learning C and I understand that to get rid of most implicit declaration warnings, you add the prototype header at the beginning. But I'm confused as to what you do when you have outside methods being used in your code.

This is my code when I'm using the outside methods

 #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;

}

The methods printArray, insert, and delete are all in the form of: printArray.o, insert.o, delete.o

This is how I compiled my program: gcc -Wall insert.o delete.o printArray.o q1.c

and I get these warnings:

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'

I've tried including this in headers but I get errors saying file or directory not found.

Any help appreciated.

网友答案:

Put them in a header file foo.h like so:

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

then include that file in your source

 #include "foo.h"
网友答案:

You need to include the correct headers to get rid of such warnings.

If you get a "file not found" error, try to include them as

#include "myheader.h"

and put your header files in the same directory as your source code.

Generally speaking, #include "file" is for programmer-defined headers while #include <file> is or standard headers.

网友答案:

You should be able to just put in the function prototype at the top of the file like you do for other functions in the same file. The linker should take care of the rest.

网友答案:

Where did you get those .o files from? If you have written them yourself, then you should create the corresponding .h files. If you got these files from somewhere else, then you should search for the headers in the same place.

网友答案:

If all called functions are written before the main() function the compiler will know their name, return type and parameter signature and can match all three of these properties with each following function invocation.

Some programmers like to write a function signature first, and do the implementation at a later time.

The only time a function declaration is essential is when using co-routines: functionA invokes functionB which in turn invokes functionA.

Done as follows:

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);
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: