Somehow I'm violating the one definition rule
I am getting errors with the linker such as:
开发者_运维知识库osd.o(.ndata+0x514):C:\Documents and Settings\Thomas\My Documents\PIC\dsPIC33FJ128GP802\On Screen Display\osd.c: multiple definition of `video_buff_vis_num'
main.o(.ndata+0x0):C:\Documents and Settings\Thomas\My Documents\PIC\dsPIC33FJ128GP802\On Screen Display\main.c: first defined here
osd.o(.ndata+0x515):C:\Documents and Settings\Thomas\My Documents\PIC\dsPIC33FJ128GP802\On Screen Display\osd.c: multiple definition of `video_buff_draw_num'
main.o(.ndata+0x1):C:\Documents and Settings\Thomas\My Documents\PIC\dsPIC33FJ128GP802\On Screen Display\main.c: first defined here
osd.o(.ndata+0x516):C:\Documents and Settings\Thomas\My Documents\PIC\dsPIC33FJ128GP802\On Screen Display\osd.c: multiple definition of `vid_format'
main.o(.ndata+0x2):C:\Documents and Settings\Thomas\My Documents\PIC\dsPIC33FJ128GP802\On Screen Display\main.c: first defined here
osd.o(.ndata+0x518):C:\Documents and Settings\Thomas\My Documents\PIC\dsPIC33FJ128GP802\On Screen Display\osd.c: multiple definition of `vid_line'
main.o(.ndata+0x4):C:\Documents and Settings\Thomas\My Documents\PIC\dsPIC33FJ128GP802\On Screen Display\main.c: first defined here
This is bugging me, because in the source code I have include guards around the only place these definitions could come from.
#ifndef OSD_H
#define OSD_H
// code here, including definitions for the above
#endif // OSD_H
This is getting on my nerves. I've cleaned, rebuilt, and tried again. I even started a new project from scratch with the same files and I'm getting exactly the same problem! Would anyone please enlighten me on why this isn't working! :)
Compiling with PIC-GCC v3.23 (a version of GCC for PIC24F/H and dsPIC30F/33F microcontrollers.)
Let me know if anyone wants to see the sources of more files. I didn't want to overcrowd this page.
If you include this header in more than 1 .c
file you will have multiple definitions. And you have main.c
and osd.c
.
The .h
is the right place for functions and extern
data declarations. But for a variable you will have to pick one of the source files. the #defined
guards don't change that.
Also see this question. And this answer describes the standard pattern .
And a little more explanation/analysis:
1) Each .c
source file is compiled independently. The guards only protect from reading a header file twice during 1 compile.
2) The error you are getting is a linker (not compiler) error.
When your header defines a variable, the compiler will treat it as a definition in each separate run. The linker will detect the multiple instances.
What does your include file contain?
int video_buff_vis_num;
extern int video_buff_vis_num;
The former or the latter? The former doesn't define the variable but allocates memory for it while the latter just says "somewhere is a variable with that name".
You must have only one place where it is allocated but you can have many definitions.
精彩评论