2

I was looking at this question here : How do I use extern to share variables between source files? followed the manual . but still I get Linker errors ... Would love to get some help and explanation why it happens..

I have 2 .c files and one header file :

------check.h----

#ifndef check
#define check

extern int num;

 #endif

----check.c----

   #include "check.h"
   #include <stdio.h>



   int func(int x, int y)
   {
int z = x+y;
return z;
   }
   void printnum()
   {
num++;
printf("%d",num);
   }

----ynnynyny.c----

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include "check.h"
#include "check.c"


int num = 10;
int main() 
{ 
printf("num before is : %d\n",num);
printnum();
printf("num now is : %d",num);
getchar();


return 0; 
}

I keep getting these errors :

1>  ynnyny.c
1>  check.c
1>  Generating Code...
1>ynnyny.obj : error LNK2005: _func already defined in check.obj
1>ynnyny.obj : error LNK2005: _printnum already defined in check.obj

I wrote the #ifndef stuff and also the extern declaration, so what Is the problem?

thanks!

Community
  • 1
  • 1
user1391863
  • 137
  • 3
  • 12

1 Answers1

6

don't include "check.c" in ynnynyny.c

Josh Petitt
  • 9,371
  • 12
  • 56
  • 104
  • 2
    Also don't name your files ynnynyny.c – Shahbaz Jul 19 '12 at 17:55
  • @KevinVermeer, I'd agree, especially if you're a newbie. I have actually included .c files in very special situations (mainly if I wanted to "wrap" a .c in another file that adapted its methods). But then you have to make sure the compiler isn't compiling both the inner .c file and the outer .c file. – Josh Petitt Jul 19 '12 at 18:00
  • but how will main from one C file wll know about the func in the other C file? – user1391863 Jul 19 '12 at 18:01
  • For the OP, when your compiler compiled check.c, there were two functions defined in the global namespace. When you included this file in ynnynyny.c it tried to define them again. Another hack would be to make the functions in check.c static. This would limit their namespace. Then you would have two separate definitions for the functions, one that existed in check.c that would never get called (probably would get a warning about this) and one that existed in ynnynyny.c. Sometimes you can use this to your advantage (think "mixin" pattern), but most of the time it is not worth it. – Josh Petitt Jul 19 '12 at 18:04
  • 1
    @user1391863 - During compilation, it won't know about the function. It will simply know the name of the function, as declared in the .h file. The compiler can preprocess, convert to assembly, and generate `main.obj`, `check.obj`, and `ynnynyny.obj` (as shown in your compiler's output) files in an 'obj/' directory. These (binary) files will state that they expect to see functions with certain names like 'func' and 'printf' defined externally. The **linker** loads all the `*.obj` files and resolves these external links. – Kevin Vermeer Jul 19 '12 at 18:16
  • It looks like you're using MSVC, pass `/VERBOSE` to the linker for details of this process. (see http://msdn.microsoft.com/en-us/library/wdsk6as6.aspx for how to do this) – Kevin Vermeer Jul 19 '12 at 18:18