2

Recently I'm working on a project with Qt/C++/xml and some other stuff. I have to get some string values form a xml file. For that I'm using

#include "pugixml.hpp".

But when i'm using pugi::xml_document document; , it give an error as follow.

undefined reference to 'pugi::xml_document::xml_document()'

As I saw in this link I have to use pugixml.cpp while compiling. Anyone knows how to add pugixml.cpp to Qt project?

I'm using,

Qt Creator 4.0.2 Based on Qt 5.7.0 (GCC 4.9.1 20140922(Red Hat 4.9.1-10), 63 bit)

Champika
  • 65
  • 1
  • 11
  • Since you are working with Qt, it offers many classes for working with XML, take a look at http://doc.qt.io/qt-5/xml-processing.html, why do you want to use a 3rd party library? – ManuelH Nov 24 '16 at 08:16
  • @ManuelH I think the Qt xml implementations are a little more clumsy than other xml implementations. – voidpointercast Feb 21 '17 at 12:16

2 Answers2

9

You can fix this by editing the file called pugiconfig.hpp and uncommenting the line here:

// Uncomment this to switch to header-only version
//#define PUGIXML_HEADER_ONLY

Change it to:

// Uncomment this to switch to header-only version
#define PUGIXML_HEADER_ONLY

Now you don't need to include pugixml.cpp directly, the header will become self-contained.

Galik
  • 47,303
  • 4
  • 80
  • 117
  • 2
    Thank you - most useful. – Tim Seed Feb 12 '18 at 06:52
  • I did the above and still get "/usr/include/pugixml.hpp:1407:25: fatal error: pugixml.cpp: No such file or directory [build] 1407 | # define PUGIXML_SOURCE "pugixml.cpp"" – Avrdan Oct 10 '22 at 09:33
  • @Avrdan I think you still have to put the `pugixml.cpp` file in the same directory as the `pugixml.hpp` header. – Galik Oct 10 '22 at 11:37
  • @Galik Ok thanks. I switched the flag back and in Cmake I just added pugixml to target_link_libraries. This approach works. – Avrdan Oct 10 '22 at 12:18
2

I believe this error is a linker error. That means you have added the function declaration but not the function definition.

Therefore, download 'pugixml' and list the pugixml.cpp source file in QT. The function definition should be inside this .cpp file.

Then your compiler will be able to find the definition of the function.

It should solve the issue.

  • Yeah, it worked. Thank you for the answer. I downloaded 'pugixml' source and build it. Then I copy the `pugixml.cpp` file into the project. And the "pugixml" source in to `/usr/include` folder. Also I change the path of `#include "pugixml.hpp"` to `#include "pugixml_1_7/src/pugixml.hpp"` including the `pugixml.hpp` file location (/usr/include/pugixml_1_7/src/pugixml.hpp). Now it works fine. – Champika Nov 24 '16 at 06:40