Многократное включение GetPot - PullRequest
1 голос
/ 11 октября 2019

У меня проблема с GetPot (http://getpot.sourceforge.net/),, а именно тот факт, что, когда я включаю его несколько раз, я получаю ошибку multiple definition от компоновщика.

Вот 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);
}

(все файлы GetPot, то есть GetPot, GetPot.hpp и GetPot.cpp, находятся в том же каталоге, что и main.cpp и foo.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++$

Inв любом случае, вся эта проблема ставит меня в тупик, поскольку и в GetPot.hpp, и в GetPot.cpp есть защитные устройства, поэтому множественные определения не должны возникать в первую очередь.

Что я делаю не так? включить GetPot более одного раза в набор файлов (в частности, например, при написании библиотеки)?

Большое спасибо

Mattia

1 Ответ

0 голосов
/ 31 октября 2019

GetPot должен быть включен только в один файл. В противном случае вы можете рассмотреть возможность использования функций-членов и сделать inline , что не inline .

...