Including header files problem

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Including header files problem

跳至解决方案
1,134 次查看
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 项奖励
回复
1 解答
1,130 次查看
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 项奖励
回复
3 回复数
1,123 次查看
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 项奖励
回复
1,110 次查看
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 项奖励
回复
1,131 次查看
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 项奖励
回复