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!

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

6 Comments

Also don't name your files ynnynyny.c
@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.
but how will main from one C file wll know about the func in the other C file?
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.
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.