金庆的专栏

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  423 随笔 :: 0 文章 :: 454 评论 :: 0 Trackbacks
"multiple definition of" 错误

在global.h定义了一个常量字符串,在多个cpp中包含该global.h.

// file: global.h
#ifndef GLOBAL_H
#define GLOBAL_H
const char * STR_TEST = "Hello world!";
#endif

结果链接时出现错误:
multiple definition of ‘STR_TEST’
虽然改为#define肯定行,但是尽量应该用const量取代宏定义。

这里有个概念性错误。
const 变量默认是 static 的,
但带有 const 的不一定是 const 变量.
此处 STR_TEST 并不是常量,而是指向常量字符串的变量。

改为
const char * const STR_TEST = "Hello world!";
就可以了。

不过我又觉得改为
const char STR_TEST[] = "Hello world!"
更好。


posted on 2008-08-26 20:12 金庆 阅读(10819) 评论(18)  编辑 收藏 引用 所属分类: 1. C/C++

评论

# re: "multiple definition of" 错误 2008-08-27 00:33 TheAnswer
const char * const STR_TEST = "Hello world!";
前面那个const可以去掉的 效果一样的  回复  更多评论
  

# re: "multiple definition of" 错误 2008-08-27 08:59 Kevin Lynx
概念性问题而已。

const int a = 12; 表示a 是个常量
const char * STR_TEST = "Hello world!";表示STR_TEST指向的内容是常量,但其本身(作为一个指针变量而言)不是一个常量。
所以:
char * const STR_TEST = ".." 即可
  回复  更多评论
  

# re: "multiple definition of" 错误 2008-08-27 09:09 金庆
@TheAnswer
char * const STR_TEST = "Hello world!";
STR_TEST[0] = 'X';
会通过编译,所以不能去掉前面的const.  回复  更多评论
  

# re: "multiple definition of" 错误[未登录] 2008-08-27 09:48 raof01
const 变量默认是 static 的?谁说的?

multiple definition并不是由其constness不正确而导致的。
char * const p = "....";(p为常量,存放在常量表中,链接时没问题)或者static char * p = "....";(限制p的作用域为编译单元,但p可修改)即可,因为字符串本身是字面常量,不能修改。

因此,你这里的错误是由于全局变量使用错误导致的,与const无关。按照你最初的定义,在多个cpp中包含该global.h就相当于多次定义了STR_TEST。因此链接时会出错。

const char STR_TEST[] = "Hello world!";这里有个拷贝操作,相当于多用了一倍的空间。

建议你看看“C++:一些基础知识”(http://blog.chinaunix.net/u/12783/showart_548200.html)3和4条  回复  更多评论
  

# re: "multiple definition of" 错误 2008-08-27 10:28 abettor
加了
#ifndef GLOBAL_H
#define GLOBAL_H
#endif
还出现重复定义?不解中……  回复  更多评论
  

# re: "multiple definition of" 错误 2008-08-27 10:29 theanswerzju
@金庆
会有运行时错误的这位大哥  回复  更多评论
  

# re: "multiple definition of" 错误 2008-08-27 10:30 theanswerzju
@abettor
这个防护宏只能防止一个cpp里重复包含  回复  更多评论
  

# re: "multiple definition of" 错误 2008-08-27 10:56 komac
文章内容有误导性,定义一个常量在多个文件中使用如果是定义在头文件中就会出现multi definition的错误,只需要把你想用的变量定义在cpp文件里面,然后在h文件里面用external关键字声明即可。出现多次定义的原因是多次包含,虽然用了ifndef define endif 也不可以避免在多个不想干文件里面包含该头文件所产生的错误。  回复  更多评论
  

# re: "multiple definition of" 错误[未登录] 2008-08-27 10:57 raof01
@abettor,@theanswerzju
IMHO,你们对于变量的定义理解存在问题。参考“C/C++:如何理解复杂的声明”(http://blog.chinaunix.net/u/12783/showart_378340.html)。

变量定义一次就分配一次存储。多次包含global.h,则多次为STR_TEST分配空间。编译器正常是因为分开编译。链接异常是因为有多个存储叫做STR_TEST,通过STR_TEST引用存储存在二义性。

而且inclusion guard是防止一个h被重复包含,而不是在一个cpp里重复包含。最基础的书籍关于这点也是很清楚的。

与inclusion guard无关。  回复  更多评论
  

# re: "multiple definition of" 错误[未登录] 2008-08-27 10:58 raof01
@komac
你没有理解博主的意思。  回复  更多评论
  

# re: "multiple definition of" 错误[未登录] 2008-08-27 10:59 raof01
OMG!看了这么多留言才知道,只有kevin lynx和博主——当然也包括本人——知道讨论的是什么问题。

IMHO,其他人该好好巩固一下基础了。  回复  更多评论
  

# re: "multiple definition of" 错误 2008-08-27 13:23 金庆
@raof01
看了你的文章,很好,学到了 bitwise constant.

const char STR_TEST[] = "Hello world!";
确实多用了空间, 看来还是用 const char * const 好,这样只需复制指针。

const 变量默认是 static,应该是C++标准说的吧。不然我的代码就会编译出错。   回复  更多评论
  

# re: "multiple definition of" 错误[未登录] 2008-08-27 13:43 raof01
@金庆
c++标准哪里有?把原话贴过来啊。  回复  更多评论
  

# re: "multiple definition of" 错误[未登录] 2008-08-27 13:46 raof01
考虑这种情况:
void foo()
{
int const a = 10;
a++;
}

如果const默认是static,那么上面的函数应该等价于:
void foo()
{
static int a = 10;
a++;
}

可能吗?  回复  更多评论
  

# re: "multiple definition of" 错误[未登录] 2008-08-27 13:49 raof01
考虑这种情况:
void foo()
{
int const a = 10;
}

如果const默认是static,那么上面的函数应该等价于:
void foo()
{
static int a = 10;
}

可能吗?  回复  更多评论
  

# re: "multiple definition of" 错误 2008-08-27 19:02 金庆
@raof01
更准确的说法可能是:C++中const默认为内部链接的。
我本来不甚了了,现在查了C++标准终于清楚了。

C++ 2003 标准下载自:
http://d.download.csdn.net/down/167339/i2005reg

原文如下:

Annex C
3.5

Change: A name of file scope that is explicitly declared const, and not explicitly declared extern, has internal linkage, while in C it would have external linkage

Rationale: Because const objects can be used as compile-time values in C + +, this feature urges programmers to provide explicit initializer values for each const. This feature allows the user to put const objects in header files that are included in many compilation units.

  回复  更多评论
  

# re: "multiple definition of" 错误 2008-08-27 21:18 komac
@金庆

This feature allows the user to put const objects in header files that are included in many compilation units.
的确无知了,不知道const还有这个用法,惭愧惭愧  回复  更多评论
  

# re: "multiple definition of" 错误[未登录] 2008-08-28 09:24 raof01
看来我的理解也不够深刻……  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理