0

I have a project with many .cpp files. In some of these files I need to use pugixml in wchar_t mode and some of them are already implemented using char and pugixml in usual mode. I know I have to use definition

#define PUGIXML_WCHAR_MODE

When I trying to do like this in files that use wchar_t mode:

#define PUGIXML_WCHAR_MODE
*some code*
#undef

it gives me linking errors. What do I do wrong?

4

1 回答 1

1

pugixml is not designed to work this way. Ideally you can try to change the code to use one combination of compilation flags (functions to perform UTF-8 <-> wchar_t conversion, pugi::as_utf8 and pugi::as_wide, might help).

There is an unsupported way that should work, but is not particularly pretty - you can rename pugi namespace for wchar configuration.

To do that, you'd have to create a special header, i.e. pugixmlw.hpp, that you'll include for wide-char configuration; this header contents should be roughly as follows:

#ifndef HEADER_PUGIXMLW_HPP
#define HEADER_PUGIXMLW_HPP

#define PUGIXML_WCHAR_MODE
#define pugi pugiw

#undef HEADER_PUGIXML_HPP
#undef HEADER_PUGICONFIG_HPP
#undef SOURCE_PUGIXML_CPP

#undef PUGIXML_TEXT
#undef PUGIXML_CHAR

#include "pugixml.hpp"

#undef PUGIXML_TEXT
#undef PUGIXML_CHAR

#undef HEADER_PUGIXML_HPP
#undef HEADER_PUGICONFIG_HPP
#undef SOURCE_PUGIXML_CPP

#undef pugi
#undef PUGIXML_WCHAR_MODE

#endif

If you're using header-only pugixml variant, that should be it; if you're compiling pugixml.cpp, you'll have to add an extra source file, pugixmlw.cpp, with the following contents:

#define PUGIXML_WCHAR_MODE    
#define pugi pugiw
#include "pugixml.cpp"

That should be it.

于 2013-01-15T05:39:45.310 回答