構建c代碼時出現錯誤當我嘗試使用-D構建我的c代碼文件時,出現以下錯誤。但是,如果我在沒有-D的情況下構建它,它就可以工作。我不知道爲什麼。謝謝。 (我的機器是ubuntu12.10,32位)使用-D
gcc c1.c c2.c -D DEBUG
/tmp/ccX04EIf.o:(.data+0x0): multiple definition of `g'
/tmp/cc0j9MoU.o:(.data+0x0): first defined here
collect2: ld returned 1 exit status
這裏是我的源代碼:
global.h
#ifdef DEBUG
int g = 23;
static int init = 1;
#else
int g;
static int init = 0;
#endif
c1.c
#include "global.h"
int f() {
return g + 1;
}
c2.c
#include <stdio.h>
#include "global.h"
int main() {
if (!init) {
g = 37;
}
int t = f();
printf("calling f yields %d\n", t);
return 0;
}
爲什麼'-D'和'DEBUG'之間的空格? –
@HowChen重要嗎? # – RedX
@Jost,但他說,它的工作原理沒有-D –