Multiple inclusion of GetPot

Viewed 180

I'm facing a problem with GetPot (http://getpot.sourceforge.net/), namely the fact that when I include it more than once I get a multiple definition error by the linker.

Here's a MWE:

file main.cpp:

#include "GetPot"

int main()
{
    return 0;
}

file foo.cpp:

#include "GetPot"

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

(all GetPot files, that is GetPot, GetPot.hpp and GetPot.cpp, are in the same directory as main.cpp and foo.cpp).

Compiling with g++ main.cpp foo.cpp, I get:

/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++$

On the other hand, compiling GetPot.cpp into an object file GetPot.o and then simply including GetPot.hpp in my source files will not work either, since GetPot::operator() is only declared in GetPot.cpp, hence the error:

/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++$

In any case, this whole problem baffles me, since both GetPot.hpp and GetPot.cpp have include guards, so multiple definitions should not happen in the first place.

What am I doing wrong? Is there really no way to include GetPot more than once in a collection of files (in particular, for example, when writing a library)?

Thank you very much

Mattia

1 Answers

GetPot should only be included in one file. Otherwise, you might consider going through the member functions and make inline what is not inline.

Related