GetPot 的多重包含

Multiple inclusion of GetPot

我在使用 GetPot (http://getpot.sourceforge.net/) 时遇到问题,即当我多次包含它时,链接器出现 multiple definition 错误。

这是一个 MWE:

文件main.cpp:

#include "GetPot"

int main()
{
    return 0;
}

文件foo.cpp:

#include "GetPot"

void foo()
{
    GetPot configurationFileParser("foobar");
    double bar = configurationFileParser("bar", 0.0);
}

(所有 GetPot 文件,即 GetPotGetPot.hppGetPot.cpp,与 main.cppfoo.cpp 位于同一目录中)。

g++ main.cpp foo.cpp 编译,我得到:

/tmp/ccD22ma0.o: In function `GetPot::__constraint_check(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const*, bool) const':
foo.cpp:(.text+0x0): multiple definition of `GetPot::__constraint_check(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const*, bool) const'
/tmp/ccxM8JHn.o:main.cpp:(.text+0x0): first defined here
/tmp/ccD22ma0.o: In function `GetPot::__constraint_check_OR(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const**) const':
foo.cpp:(.text+0x5ca): multiple definition of `GetPot::__constraint_check_OR(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const**) const'
/tmp/ccxM8JHn.o:main.cpp:(.text+0x5ca): first defined here
/tmp/ccD22ma0.o: In function `GetPot::__constraint_check_AND(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const**) const':
foo.cpp:(.text+0x752): multiple definition of `GetPot::__constraint_check_AND(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const**) const'
/tmp/ccxM8JHn.o:main.cpp:(.text+0x752): first defined here
/tmp/ccD22ma0.o: In function `GetPot::__constrain_check_EQUAL_STRING(char const*, char const**) const':
foo.cpp:(.text+0x692): multiple definition of `GetPot::__constrain_check_EQUAL_STRING(char const*, char const**) const'
/tmp/ccxM8JHn.o:main.cpp:(.text+0x692): first defined here
/tmp/ccD22ma0.o: In function `GetPot::__constraint_check_PRIMARY(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const**) const':
foo.cpp:(.text+0x81e): multiple definition of `GetPot::__constraint_check_PRIMARY(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const**) const'
/tmp/ccxM8JHn.o:main.cpp:(.text+0x81e): first defined here
collect2: error: ld returned 1 exit status
(ins)mattia@endor:getpot-c++$

另一方面,将 GetPot.cpp 编译成一个目标文件 GetPot.o 然后简单地将 GetPot.hpp 包含在我的源文件中也不起作用,因为 GetPot::operator() 是仅在 GetPot.cpp 中声明,因此错误:

/tmp/ccSIAz1V.o: In function `foo()':
foo.cpp:(.text+0x106): undefined reference to `double GetPot::operator()<double>(StringOrCharP, double) const'
collect2: error: ld returned 1 exit status
(ins)mattia@endor:getpot-c++$

无论如何,这整个问题都让我感到困惑,因为 GetPot.hppGetPot.cpp 都包含守卫,所以首先不应该发生多个定义。

我做错了什么?真的没有办法在文件集合中多次包含 GetPot(特别是,例如,在编写库时)吗?

非常感谢

马蒂亚

GetPot 应该只包含在一个文件中。否则,您可能会考虑遍历成员函数并使 inline 不是 inline.