我在这里看到这个问题: How do I use extern to share variables between source files? 按照手册。但我仍然得到链接器错误... 很想得到一些帮助和解释为什么会发生..
我有2个.c文件和一个头文件:
------ ---- 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;
}
我一直收到这些错误:
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
我写了#ifndef的东西以及extern声明,那么问题是什么?
谢谢!
答案 0 :(得分:6)
不要在ynnynyny.c中包含“check.c”