在头文件中定义 [英] Definition in Header files

查看:67
本文介绍了在头文件中定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在命令头

文件中添加定义时遇到了问题。我已经定义了一个包含大量全局

常量的头文件,我在所有C文件中使用它们。


例如我的sample.h文件看起来像这样


/ *************


文件:sample.h

**************** /

const int a1 = 0;

const int a2 = 0;

/ ****样本结束.h ********* /

我的源代码是这样的


/ *****************

文件:sample1.c


*********** /

#include" sample.h"


int main()

{

}


/ *****样本结束1.c ************ /


/ ***************

文件:sample2.c

** ************ /


#include" sample.h


int main()

{

}


/ ******* sample2.c结束********* * /


现在问题是在链接期间它说多个<。的定义br />
符号a1和a2。我可以通过一个C文件来避免这个问题,其中
包含声明的定义和H文件。但有没有任何

方式我可以避免这样做。请告诉我。

I have problems faced in adding definitions in the command header
file. I have defined a header file which includes huge set of global
constants and I am using them in all the C files.

For example my sample.h file looks like this

/*************

File : sample.h

****************/
const int a1 = 0;
const int a2 = 0;
/****End of sample.h*********/
My source code looks like this

/*****************
File:sample1.c

***********/
#include "sample.h"

int main()
{
}

/*****End of sample1.c************/

/***************
File : sample2.c
**************/

#include "sample.h

int main()
{
}

/*******End of sample2.c**********/

Now the problem is during linking it says multiple definition of
symbols a1 and a2. I can avoid this problem by having a C file which
contains the definitions and H file for declarations. But is there any
way I can avoid doing this. Please let me know.

推荐答案

10月23日上午6:39,Madhur< madhurr ... @ gmail.comwrote :
On Oct 23, 6:39 am, Madhur <madhurr...@gmail.comwrote:

我在命令头中添加定义时遇到问题

文件。我已经定义了一个包含大量全局

常量的头文件,我在所有C文件中使用它们。


例如我的sample.h文件看起来像这样


/ *************


文件:sample.h

**************** /


const int a1 = 0;

const int a2 = 0;


/ ****样本结束.hh ***** /


我的源代码是这样的


/ *****************

文件:sample1。 c


*********** /

#include" sample.h"


int main()

{


}


/ *****样本结束1 .c ************ /


/ ***************

文件:sample2.c

************** /


#include" sample.h


int main()

{


}


/ * ******样本结束2.c ********** /


现在问题是在链接期间它说是multip定义

符号a1和a2。我可以通过一个C文件来避免这个问题,其中
包含声明的定义和H文件。但有没有任何

方式我可以避免这样做。请告诉我。
I have problems faced in adding definitions in the command header
file. I have defined a header file which includes huge set of global
constants and I am using them in all the C files.

For example my sample.h file looks like this

/*************

File : sample.h

****************/

const int a1 = 0;
const int a2 = 0;

/****End of sample.h*********/

My source code looks like this

/*****************
File:sample1.c

***********/
#include "sample.h"

int main()
{

}

/*****End of sample1.c************/

/***************
File : sample2.c
**************/

#include "sample.h

int main()
{

}

/*******End of sample2.c**********/

Now the problem is during linking it says multiple definition of
symbols a1 and a2. I can avoid this problem by having a C file which
contains the definitions and H file for declarations. But is there any
way I can avoid doing this. Please let me know.



我认为你的问题是,根据扩展名,

这些是C而不是C ++文件。

在C ++中,a1和a2有内部联系所以你应该没有问题,但是C,IIRC中的规则是不同的。

所以,将文件重命名为cpp或以其他方式将它们编译为

C ++。如果他们必须保持C,则将静态保留为静态。

前面的变量定义。

I think your problem is that, based on the extension,
these are C rather than C++ files.
In C++ a1 and a2 have internal linkage so you should have
no problem, but the rules in C, IIRC, are different.
So, rename the files to cpp or otherwise compile them as
C++. If they must remain C, put "static" in front of
the variable definitions.




" tragomaskhalos" < da ************* @ logicacmg.comwrote in message

news:11 ***************** *****@q3g2000prf.googlegro ups.com ...

"tragomaskhalos" <da*************@logicacmg.comwrote in message
news:11**********************@q3g2000prf.googlegro ups.com...

10月23日上午6:39,Madhur< madhurr ... @ gmail。 comwrote:
On Oct 23, 6:39 am, Madhur <madhurr...@gmail.comwrote:

>我在命令头
文件中添加定义时遇到了问题。我已经定义了一个包含大量全局
常量的头文件,我在所有C文件中使用它们。

例如我的sample.h文件看起来像这样

/ *************

文件:sample.h

*********** ***** /

const int a1 = 0;
const int a2 = 0;

/ **** sample.h的结尾** ******* /

我的源代码看起来像这样

/ *****************
文件:样本1

*********** /
#include" sample.h"

int main( )
{

}

/ *****样本结束.1 ************ /

/ ***************
文件:sample2.c
************** /

#include" sample.h
int main()
{

}

/ * ******样本结束2.c ********** /

现在问题出在链接期间,它表示
符号a1和a2的多个定义。我可以通过一个包含定义的C文件和声明的H文件来避免这个问题。但是有什么方法可以避免这样做。请告诉我。
>I have problems faced in adding definitions in the command header
file. I have defined a header file which includes huge set of global
constants and I am using them in all the C files.

For example my sample.h file looks like this

/*************

File : sample.h

****************/

const int a1 = 0;
const int a2 = 0;

/****End of sample.h*********/

My source code looks like this

/*****************
File:sample1.c

***********/
#include "sample.h"

int main()
{

}

/*****End of sample1.c************/

/***************
File : sample2.c
**************/

#include "sample.h

int main()
{

}

/*******End of sample2.c**********/

Now the problem is during linking it says multiple definition of
symbols a1 and a2. I can avoid this problem by having a C file which
contains the definitions and H file for declarations. But is there any
way I can avoid doing this. Please let me know.



我认为你的问题是,根据扩展名,

这些是C而不是C ++文件。

在C ++中,a1和a2有内部联系所以你应该没有问题,但是C,IIRC中的规则是不同的。

所以,将文件重命名为cpp或以其他方式将它们编译为

C ++。如果他们必须保持C,则将静态保留为静态。在

前面的变量定义。


I think your problem is that, based on the extension,
these are C rather than C++ files.
In C++ a1 and a2 have internal linkage so you should have
no problem, but the rules in C, IIRC, are different.
So, rename the files to cpp or otherwise compile them as
C++. If they must remain C, put "static" in front of
the variable definitions.



我很确定这不是问题所在。链接错误可能是因为

头文件中没有包含防护,并且头文件是

包含在同一编译单元中的两个文件中。


但如果他正在使用C ++,那么你应该是.cpp文件是正确的。

的主要原因是因为某些IDE将使用C编译器而不是C ++编译器的
扩展名为.c的文件。

-Howard

I''m pretty sure that''s not the problem. The link error is probably because
there are no include guards in the header file, and the header file is
included by two files in the same compilation unit.

But you are correct that those should be .cpp files if he''s using C++. The
main reason for that is because certain IDEs will use the C compiler instead
of the C++ compiler for files with a .c extension.

-Howard


Howard写道:
Howard wrote:

" tragomaskhalos" < da ************* @ logicacmg.comwrote in message

news:11 ***************** *****@q3g2000prf.googlegro ups.com ...
"tragomaskhalos" <da*************@logicacmg.comwrote in message
news:11**********************@q3g2000prf.googlegro ups.com...

> 10月23日上午6:39,Madhur< madhurr ... @ gmail.comwrote:
>On Oct 23, 6:39 am, Madhur <madhurr...@gmail.comwrote:

>>我在命令头
文件中添加定义时遇到了问题。我已经定义了一个包含大量全局
常量的头文件,我在所有C文件中使用它们。

例如我的sample.h文件看起来像这样

/ *************

文件:sample.h

*********** ***** /

const int a1 = 0;
const int a2 = 0;

/ **** sample.h的结尾** ******* /

我的源代码看起来像这样

/ *****************
文件:样本1

*********** /
#include" sample.h"

int main( )
{

}

/ *****样本结束.1 ************ /

/ ***************
文件:sample2.c
************** /

#include" sample.h
int main()
{

}

/ * ******样本结束2.c ********** /

现在问题出在链接期间,它表示
符号a1和a2的多个定义。我可以通过一个包含定义的C文件和声明的H文件来避免这个问题。但是有什么方法可以避免这样做。请告诉我。
>>I have problems faced in adding definitions in the command header
file. I have defined a header file which includes huge set of global
constants and I am using them in all the C files.

For example my sample.h file looks like this

/*************

File : sample.h

****************/

const int a1 = 0;
const int a2 = 0;

/****End of sample.h*********/

My source code looks like this

/*****************
File:sample1.c

***********/
#include "sample.h"

int main()
{

}

/*****End of sample1.c************/

/***************
File : sample2.c
**************/

#include "sample.h

int main()
{

}

/*******End of sample2.c**********/

Now the problem is during linking it says multiple definition of
symbols a1 and a2. I can avoid this problem by having a C file which
contains the definitions and H file for declarations. But is there
any way I can avoid doing this. Please let me know.


我认为你的问题是,基于扩展,这些是C而不是C ++文件。
在C ++中,a1和a2有内部联系,所以你应该没有问题,但是C,IIRC中的规则是不同的。
因此,将文件重命名为cpp或以其他方式将它们编译为C ++。如果他们必须保持C,则将静态保留为静态。
变量定义。


I think your problem is that, based on the extension,
these are C rather than C++ files.
In C++ a1 and a2 have internal linkage so you should have
no problem, but the rules in C, IIRC, are different.
So, rename the files to cpp or otherwise compile them as
C++. If they must remain C, put "static" in front of
the variable definitions.



我很确定这不是问题所在。链接错误可能是

,因为头文件中没有包含保护,并且

头文件包含在同一编译单元中的两个文件中。


I''m pretty sure that''s not the problem. The link error is probably
because there are no include guards in the header file, and the
header file is included by two files in the same compilation unit.



*同样的编译单元*?单独的.c文件应该是单独的

编译,不是吗?

What *same compilation unit*? Separate .c files should mean separate
compilation, no?


但你是正确的那些应该是.cpp文件[..]
But you are correct that those should be .cpp files [..]



V

-

请在通过电子邮件回复时删除资金'A'

我没有回复最热门的回复,请不要问

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于在头文件中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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