编写仅标题模板库时出现"错误的多重定义"

Fly*_*Hat 1 c++ header-only

我的项目类似于此设置,当我需要使用my_template_library.hmain_class.h.

main.cpp中

#include "main_class.h"

int main()
{
    MainClass m;
    return m.exec();
}
Run Code Online (Sandbox Code Playgroud)

main_class.h

#ifndef MAIN_CLASS_H
#define MAIN_CLASS_H

#include "my_template_library.h"

class MainClass {
public:
    MainClass();
    int exec();
};

#endif // MAIN_CLASS_H
Run Code Online (Sandbox Code Playgroud)

main_class.cpp

#include <iostream>

#include "main_class.h"

MainClass::MainClass(){}

int MainClass::exec()
{
    std::cout << "exec!" << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

my_template_library.h

#ifndef MY_TEMPLATE_LIBRARY_H
#define MY_TEMPLATE_LIBRARY_H

#include <iostream>

//#pragma message ("I'm being included past the include guards!")

class MyTemplateLibrary
{
public:
    MyTemplateLibrary();

    void function();
};

MyTemplateLibrary::MyTemplateLibrary(){}

void MyTemplateLibrary::function()
{
    std::cout << "function called!" << std::endl;
}

#endif // MY_TEMPLATE_LIBRARY_H
Run Code Online (Sandbox Code Playgroud)

在模板中只有头库我写我第一次在一个类中声明一切,然后在类外定义的一切,喜欢什么,当你把班级分成你通常会做.h.cpp代码,但.cpp文件的末尾添加.h,里面包括警卫.只要我的模板库只包含一次就可以正常工作,但是当它开始被包含在内时,我会遇到一些非常令人困惑的问题.

$ g++ -o test main.cpp main_class.h main_class.cpp my_template_library.h 
/tmp/ccuFlEDZ.o: In function `MyTemplateLibrary::MyTemplateLibrary()':
main_class.cpp:(.text+0x0): multiple definition of `MyTemplateLibrary::MyTemplateLibrary()'
/tmp/ccZikorv.o:main.cpp:(.text+0x0): first defined here
/tmp/ccuFlEDZ.o: In function `MyTemplateLibrary::MyTemplateLibrary()':
main_class.cpp:(.text+0x0): multiple definition of `MyTemplateLibrary::MyTemplateLibrary()'
/tmp/ccZikorv.o:main.cpp:(.text+0x0): first defined here
/tmp/ccuFlEDZ.o: In function `MyTemplateLibrary::function()':
main_class.cpp:(.text+0xa): multiple definition of `MyTemplateLibrary::function()'
/tmp/ccZikorv.o:main.cpp:(.text+0xa): first defined here
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

我对发生了什么很困惑.我已经添加了#pragma messagemy_template_library.h来解释一下这个,你在这里看到的代码注释掉了.当我取消注释并运行我得到的代码

$ g++ -o test main.cpp main_class.h main_class.cpp my_template_library.h 
In file included from main_class.h:4:0,
                 from main.cpp:1:
my_template_library.h:6:63: note: #pragma message: I'm being included past the include guards!
 #pragma message ("I'm being included past the include guards!")
                                                           ^
In file included from main_class.h:4:0:
my_template_library.h:6:63: note: #pragma message: I'm being included past the include guards!
 #pragma message ("I'm being included past the include guards!")
                                                           ^
In file included from main_class.h:4:0,
             from main_class.cpp:3:
my_template_library.h:6:63: note: #pragma message: I'm being included past the include guards!
 #pragma message ("I'm being included past the include guards!")
                                                           ^
my_template_library.h:6:63: note: #pragma message: I'm being included past the include guards!
 #pragma message ("I'm being included past the include guards!")
                                                           ^
/tmp/ccmawdhP.o: In function `MyTemplateLibrary::MyTemplateLibrary()':
main_class.cpp:(.text+0x0): multiple definition of `MyTemplateLibrary::MyTemplateLibrary()'
/tmp/cc4XSnui.o:main.cpp:(.text+0x0): first defined here
/tmp/ccmawdhP.o: In function `MyTemplateLibrary::MyTemplateLibrary()':
main_class.cpp:(.text+0x0): multiple definition of `MyTemplateLibrary::MyTemplateLibrary()'
/tmp/cc4XSnui.o:main.cpp:(.text+0x0): first defined here
/tmp/ccmawdhP.o: In function `MyTemplateLibrary::function()':
main_class.cpp:(.text+0xa): multiple definition of `MyTemplateLibrary::function()'
/tmp/cc4XSnui.o:main.cpp:(.text+0xa): first defined here
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

因此头文件包含在:

  1. main.cpp - > main_class.h
  2. main_class.h
  3. main_class.cpp - > main_class.h
  4. my_template_library.h(单凭?)

所以,我的问题是:

  1. 为什么包括警卫没有帮助?
  2. 如何防止这种情况发生?

Dre*_*ann 6

为什么包括警卫没有帮助?

包含防护措施可防止同一编译单元中的冗余代码.

您收到有关声称拥有相同函数定义的不同编译单元的链接器错误.

如何防止这种情况发生?

您需要创建这些非模板成员函数inline以避免违反单一定义规则.

一种方法是明确地将它们内联声明.

inline MyTemplateLibrary::MyTemplateLibrary(){}
Run Code Online (Sandbox Code Playgroud)

或者,类定义中定义的函数是隐式内联的.

class MyTemplateLibrary
{
public:
    MyTemplateLibrary() {}

    void function()
    {
        std::cout << "function called!" << std::endl;
    }
};
Run Code Online (Sandbox Code Playgroud)