2
我在這裏看這個問題: How do I use extern to share variables between source files? 按照手冊。但我仍得到鏈接錯誤...... 很想得到一些幫助,並解釋爲什麼會發生..extern聲明和頭文件在C
我有2個.c文件和一個頭文件:
------檢查。 ħ----
#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聲明,那麼,是什麼問題呢?
謝謝!
更重要的是,從來沒有'#include'任何C文件! – 2012-07-19 17:54:45
也不要命名你的文件ynnynyny.c – Shahbaz 2012-07-19 17:55:47
@KevinVermeer,我同意,特別是如果你是一個新手。在非常特殊的情況下,我實際上已經包含了.c文件(主要是如果我想在另一個修改其方法的文件中「包裝」.c文件)。但是,您必須確保編譯器不編譯內部.c文件和外部.c文件。 – 2012-07-19 18:00:16