【问题标题】:Multiple inclusion of GetPot多次包含 GetPot
【发布时间】:2019-10-11 09:11:05
【问题描述】:

我遇到了 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(特别是,例如,在编写库时)?

非常感谢

马蒂亚

【问题讨论】:

    标签: c++ multiple-definition-error


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复!我明白你的意思,虽然这似乎是一种奇怪的行为,不是吗?这有效地迫使用户只能解析单个源文件中的配置文件,而不是有可能跨库使用解析功能。
    • 是的,这种行为很奇怪。您的评论应转化为错误或功能请求。
    • 好的,我会做的!真的非常感谢!
    猜你喜欢
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    • 1970-01-01
    相关资源
    最近更新 更多