***错误L104:多个公共定义 [英] *** ERROR L104: MULTIPLE PUBLIC DEFINITIONS

查看:1497
本文介绍了***错误L104:多个公共定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在一个名为ADeclaration.h的文件中声明了所有声明,并将此文件包含在我的所有模块C文件中.



我有

I have Declared all my declaration in one file called ADeclaration.h and included this file in all my module C files.

i.e.

I have

MAIN.c (MAIN)
 Port_IO.c (PORT_IO)
 STORAGE.c (STORAGE)
 TEMPERATURE.c (TEMPERATURE)
 CONVERT.c (CONVERT)
 UART.c (UART)
 RTC.c (RTC)
 I2C.c (I2C)
 ISR.c(ISR)
 FLASH.c (FLASH)



该文件的所有声明都在Adeclaration.h

现在我用
获取我的project.M51 程式大小:data = 157.5 xdata = 5988代码= 11148
链接/定位运行完成. 29警告(S),294错误(S)

并且没有创建目标

大多数错误就像



All the declaration of this files are in Adeclaration.h

Now I am getting my project.M51 with
Program Size: data=157.5 xdata=5988 code=11148
LINK/LOCATE RUN COMPLETE. 29 WARNING(S), 294 ERROR(S)

and the target is not created

most of the error are like

*** ERROR L104: MULTIPLE PUBLIC DEFINITIONS
    SYMBOL:  LOCAL
    MODULE:  STORAGE.obj

推荐答案

链接器抱怨多个定义,而不是声明.
如果需要在多个来源中使用符号(例如变量),则必须:
  • 在一个单一来源中定义.
  • 声明它,在所有其他来源中也定义为extern(您可以包括用于此目的的头文件.)
The linker complains about multiple definitions, not declarations.
If you need to use a symbol (for instance a variable) in multiple sources then you have to:
  • Define it in one, single source.
  • Declare it, as extern in all the other sources (you may include an header file for the purpose).
#ifndef _DECL_H_
#define _DECL_H_
/* global symbol declarations */
extern int global_counter;
/*...*/
#endif



源文件 src1.c



source file src1.c

/* global symbol definition */
int global_counter; 
/*...*/



源文件 src2.c



source file src2.c

#include "decl.h"

/* use the global symbol */
int increment()
{
  global_counter++;
}
/*...*/



等等...

:)



and so on...

:)


只需将其放在您的ADeclaration.h中,以避免包含多个内容:
Simply put this in your ADeclaration.h to avoid multiple inclusions:
#ifndef ADECLARATION_H
#define ADECLARATION_H

 // Contents of ADeclaration.h 

#endif



祝你好运!



Good luck!


这篇关于***错误L104:多个公共定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆