Including header files problem

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Including header files problem

Jump to solution
1,130 Views
jussikangas
Contributor I

I have problem when including same header file from two different source files I get error from duplicate variables.

Variable is defined in PID.h

I have PID.h that is included in PID.c and main.c.

Now I trying to port my code to work in LPCXpresso.

I have been using eclipse and gcc before, and this same code and files has been working in that environment.

 

./src/main.o:(.bss.PID+0x0): multiple definition of `PID'
AHB_SRAM1: 1048 B 32 KB 3.20%
./src/PID.o:(.bss.PID+0x0): first defined here
collect2.exe: error: ld returned 1 exit status

 

Header files are located in separate holder just to keep thing clean.

It's include path is defined as:

"${workspace_loc:/${ProjName}/inc}"

 

 

0 Kudos
Reply
1 Solution
1,126 Views
ErichStyger
Senior Contributor V

Variable is defined in PID.h

That's the problem. Header files are for *declarations*, not definitions!

You probably missed the 'exern' in the header file, it shall be for example

extern int pid;

in the header file pid.h und

int pid;

in the pid.c

I hope this helps,

Erich

View solution in original post

0 Kudos
Reply
3 Replies
1,119 Views
jussikangas
Contributor I

Hello

That is  a way that I use header for declarations and in source files variables are defined.

It seems that my old tool chain handled those pit different.

 I changed according your advice and now code is build fine.

Thanks

0 Kudos
Reply
1,106 Views
ErichStyger
Senior Contributor V

It depends on the linker if it flags double definitions. I consider this as a programmer error so having it flagged as an error is a good thing.

 

0 Kudos
Reply
1,127 Views
ErichStyger
Senior Contributor V

Variable is defined in PID.h

That's the problem. Header files are for *declarations*, not definitions!

You probably missed the 'exern' in the header file, it shall be for example

extern int pid;

in the header file pid.h und

int pid;

in the pid.c

I hope this helps,

Erich

0 Kudos
Reply