views:

662

answers:

5

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 -Wextra -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 to
Testing'

What I need to do?

A: 

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.

Toji
C doesn't distingusish between header and code files.He's #included the .h into the .c, so there's no difference between that and directly putting it in the file.
McPherrinM
Sure you can put active code in the header. It's just a bad, bad idea.
dmckee
Thanks for clarifying. I do enough C++ that the differences between them blur sometimes.
Toji
Headers and #include work identically in C++ and C.
Roger Pate
A: 
tommieb75
Same error **:(**
Nathan Campos
A: 

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.

AndreyT
+1  A: 

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.

McPherrinM
A: 

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 .

vijay