【问题标题】:While using #ifndef, .h file being added multiple times使用#ifndef 时,.h 文件被多次添加
【发布时间】:2010-04-18 19:53:03
【问题描述】:

我正在尝试使用以下模式。

#ifndef TRACER_H
#include "Tracer.h"
#endif

这个is语句被添加到代码中的每个文件中,这样tracer.h只被添加一次。 我仍然收到说多个对象的错误。

Tracer.h 也包含

#ifndef TRACER_H
#define TRACER_H

这是错误; 我也试过一次 pragma:

1>Generating Code...
1>Linking...
1>LINK : \\stu05-fsrv.ad.syr.edu\akbhat$\Visual Studio 2008\Projects\Project3\Debug\Project3.exe not found or not built by the last incremental link; performing full link
1>SemiExpression.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class tracer &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAVtracer@@@Z) already defined in main.obj
1>SemiExpression.obj : error LNK2005: "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > tracer::log" (?log@tracer@@0V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) already defined in main.obj
1>Tokenizer.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class tracer &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAVtracer@@@Z) already defined in main.obj
1>Tokenizer.obj : error LNK2005: "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > tracer::log" (?log@tracer@@0V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) already defined in main.obj
1>\\stu05-fsrv.ad.syr.edu\akbhat$\Visual Studio 2008\Projects\Project3\Debug\Project3.exe : fatal error LNK1169: one or more multiply defined symbols found
1>Build log was saved at "file://\\stu05-fsrv.ad.syr.edu\akbhat$\Visual Studio 2008\Projects\Project3\Project3\Debug\BuildLog.htm"

【问题讨论】:

  • 请告诉我们错误发生的地方和周围的代码。

标签: c++ c-preprocessor


【解决方案1】:

首先,标题保护进入文件内部。它使事情变得更容易:

// some_header.h
#ifndef SOME_HEADER_INCLUDED_H
#define SOME_HEADER_INCLUDED_H

// ...

#endif

其次,这些守卫只能防止多个包含每个翻译单元。如果您有main.cpp 和foo.cpp,并且每个都包含:

#include "some_header.h"
#include "some_header.h" // again

// ...

包含保护之间的内容将仅包含一次每个单元,但将定义两次,每个单元中一次。

链接时间到来时,您会遇到多定义错误。您需要在 one 源文件中定义这些静态变量,并且只在标头中声明它。

【讨论】:

  • 我创建了 Tracer.cpp 并在该文件中定义了静态变量 log。然而,当我在多个文件中包含 Tracer.h 时,我仍然会遇到相同的错误
【解决方案2】:

也许你想了解include guards。他们应该提供您正在寻找的东西。对于您的案例,它们是众所周知的标准模式。
根据您的编译器,#pragma once 也可能很有趣。也许您在 SO 上阅读了pragma vs. guard (关于 SO 上的这两个问题还有很多好问题)。

【讨论】:

    【解决方案3】:

    我认为您忘记在您的包含保护中将#endif 放在#ifdef 之后。只需在头文件中使用此模式:

    #ifndef HEADERNAME_H
    #define HEADERNAME_H
    
    /* code */
    
    #endif
    

    此外,您可能会在头文件中声明对象实例,这些实例最终会在您的所有源文件中重复并导致链接错误。我需要查看更多代码才能了解确切的问题出在哪里,但是如果您在头文件中这样说:

    Foo myInstance;
    

    您将收到链接器错误,因为符号 myInstance 指的是多个实例(每个包含它的 .cpp 文件一个)而不是一个。相反,将上面的行放在 .cpp 文件中,并将其放在您的标题中:

    extern Foo myInstance;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-29
      • 1970-01-01
      • 2013-11-03
      • 2011-03-15
      • 2011-01-03
      • 2019-06-24
      • 2019-04-07
      相关资源
      最近更新 更多