I got "multiple definition of" error on Ubuntu 22.04 though I have not got such kind of error on Ubuntu 20.04. I put include guards on the top of the header file but it does not work. Does anyone know how to solve this problem other than downgrading to Ubuntu 20.04?
I have these three files
global.h
#ifndef GLOBAL_H
#define GLOBAL_H
int a;
int b;
#endif
a.c
#include "global.h"
#include <stdio.h>
#include<unistd.h>
void defb(void);
int main(int argc, char **argv)
{
defb();
a = 1;
printf("a=%d\n",a);
return 0;
}
b.c
#include "global.h"
#include <stdio.h>
#include<unistd.h>
void defb()
{
b = 2;
printf("b=%d\n",b);
}
And I compiled with gcc like
gcc -O a.c b.c
then I got the error below
/usr/bin/ld: /tmp/ccOgmibV.o:(.bss+0x0): multiple definition of `b'; /tmp/ccsYu4Ch.o:(.bss+0x0): first defined here
/usr/bin/ld: /tmp/ccOgmibV.o:(.bss+0x4): multiple definition of `a'; /tmp/ccsYu4Ch.o:(.bss+0x4): first defined here
collect2: error: ld returned 1 exit status
How to resolve this probelem? Thank you in advance.