为什么不包括守卫或pragma一旦工作? [英] Why aren't include guards or pragma once working?

查看:98
本文介绍了为什么不包括守卫或pragma一旦工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编译一些依赖于包含守卫来阻止对象和函数的多个定义的代码,但Visual Studio 2008给我的链接错误有多个定义。我不明白为什么,因为我之前使用的代码非常相似,它没有造成问题。我必须做一些哑巴,但我不知道它是什么。我也试图拿出包含守卫和使用#pragma一次,但我得到相同的链接错误。我应该检查什么?

I'm compiling some code that relies on include guards to prevent multiple definitions of objects and functions, but Visual Studio 2008 is giving me link errors that there are multiple definitions. I don't understand why because I've used code very similar to this before and it hasn't caused problems. I must be doing something dumb but I have no idea what it is. I also tried to take out the include guards and use #pragma once, but I get the same link errors. What should I check for?

推荐答案

如果它们是链接器错误,最可能的原因可能是非内联函数定义在标题。

If they are linker errors, the most likely cause is probably non-inline functions defined in the header.

如果在包含在多个源文件中的标题中具有非内联函数,则将在每个源文件中定义它(翻译单元),因此该函数将被多次定义,因此多个定义错误。

If you have a non-inline function in a header that is included in more than one source file, it will be defined in each of those source files ("translation units"), thus the function will be defined more than once, hence the multiple definitions error.

这篇关于为什么不包括守卫或pragma一旦工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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