开发者

Implicit Declaration Of Function

I've just organized my code by using headers, but just as I've done this, I got a warning that turned into an error when linking.

I have a code(use of a function that is inside a header) in test.c that is like this:

#include "test1.h"

/* Some code */
main()
{
   Testing();
}

And my test1.h header is like this:

void Testing();
void print(int, int, int, const char*);

And at test1.c

void Testing()
{
   print(0xF9, 27, 5, "\xC9\\xBB");
}

void print(int colour, int x, int y, const char *string)
{
   volatile char *video=(volatile char*)0xB8000 + y*160 + x*2;
   while(*string != 0)
   {
      *video=*string;
      string++;
      video++;
      *video=colour;
      video++;
   }
}

When I try to compile the code, I got this:

ubuntu@eeepc:~/Development/Test$ gcc -o test.o -c test.c -Wall -W开发者_高级运维extra -nostdlib -nostartfiles -nodefaultlibs

test.c: In function ‘main’:

test.c:11: warning: implicit declaration of function ‘Testing’

ubuntu@eeepc:~/Development/Test$

At the time it's just a simple warning, but when I try to link it...

ubuntu@eeepc:~/Development/Test$ ld -T linker.ld -o kernel.bin loader.o test.o

test.o: In function main':

test.c:(.text+0xfc): undefined reference toTesting'

What I need to do?


Edit: To reflect the OP's question I have struck out some lines of my answer despite being upvoted...

Why is kernel.c flagged up in the compiler, even though you don't have it mentioned here? Am I missing something...

gcc -o test.o -c test.c -Wall -Wextra -nostdlib -nostartfiles -nodefaultlibs
kernel.c: In function ‘main’:
kernel.c:11: warning: implicit declaration of function ‘Testing’
ubuntu@eeepc:~/Development/Test$

maybe you need to do it this way somewhere in your header file as I'm judging you want kernel to access this function:

extern void Testing();

And, take out all your functions and place them in a separate .c file, they should not be in there in the first place... for example:

Testing.c
/* all your functions here */

Testing.h
/* Only global, external variables and function prototypes */

Hope this helps, Best regards, Tom.


I can't recreate your problem. This works as expected when I try to compile your code on an Ubuntu machine (Which based on your paste, I assume you're using.)

Are you sure the #include is happening correctly?

Try using -E instead of -c to see what the whole code the compiler is trying to compile looks like.


Somewhat of a shot in the dark here, since my C is a bit rusty, but does C allow you to put function bodies in a header? I don't recall that it does. Try moving the definition of Testing() and print() into a .c file? You could also try compiling as C++ as see if that fixes it, if you don't need/want C.


You included test.h into main.c, while your declarations, according to what your wrote, are in test1.h. Note the 1 in the name.

In addition to that, you are compiling test.c and linking test.o, while in reality the name of your file is test1.c. Again, note the 1 in the name.

Edit: Now you edited the name of the file included into main.c. After the edit it is safe to assert that most of the symptoms you describe are not possble with the current versions of the files. Re-verify what you are doing, post updated disgnostic information and/or post real code.

Still, you compiler and linker lines are referring to old file names.


i donno whats causing this , but i had this problem just now . try to delete the .h file and put the declarations of the functions on the top of .c file itself .

in this case , delete the test1.h and put the declarations of functions of test1.c in test1.c. and include test1.c in test.c you wont get that warning message any more , nor the following linking errors .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜