Duplicate symbols with var defined in header & mul

2020-04-18 07:40发布

问题:

I have this header file (called ft_opp.h), that I'm trying to #include in two different .c files:

#ifndef __FT_OPP_H__
# define __FT_OPP_H__
t_opp gl_opptab[] = {{"-", &ft_sub}, \
{"+", &ft_add}, \
{"*", &ft_mul}, \
{"/", &ft_div}, \
{"%", &ft_mod}, \
{"", &ft_usage}};
#endif /* __FT_OPP_H__ */

I have no choice but to use this file unchanged to define the gl_opptab array (it's part of an exercise at school and I'm supposed to use this file as is).

However, I keep getting the following message from GCC after compilation:

duplicate symbol _gl_opptab in: /var/folders/zz/zyxvpxvq6csfxvn_n0000hvc00046v/T//ccrPWPyP.o /var/folders/zz/zyxvpxvq6csfxvn_n0000hvc00046v/T//cc2JUzLs.o ld: 1 duplicate symbol for architecture x86_64 collect2: ld returned 1 exit status make: * [ft_advanced_do-op] Error 1

I have tried including it in only one file and using extern t_opp* gl_opptab in the other .c file. But it doesn't seem to work.

How could I do that?

回答1:

extern t_opp* gl_opptab is not the same as the array of structs defined in the header. Try extern t_opp gl_opptab[]; in the other .c file. Of course the other .c file will need to know what a t_opp is, so there's at least one more header that'll need inclusion in the other .c file.