用Eclipse编译xerces-c++程序

compiling xerces-c++ program with Eclipse

本文关键字:程序 xerces-c++ 编译 Eclipse      更新时间:2023-10-16

我正在尝试在windows上使用xerces-c进行编程。

我已经成功地建立了这个库,并成功地编译了一个简单的程序,barebone,只需cmd和记事本。然而,当我试图将事情转移到eclipse时,事情有点失控了。

通过简单地拥有一个c++helloworld示例,然后将include文件包含到项目路径中并进行构建,eclipse拒绝构建项目,从而产生了很多错误,我认为这些错误主要与命名空间有关。

错误如下:

Info: Internal Builder is used for build
g++ -I...blablabla -O3 -Wall -c -fmessage-length=0 -o "src\helloworld.o" "..\src\helloworld.cpp" 
gcc -O3 -Wall -c -fmessage-length=0 -o "src\includes\xercesc\util\RefStackOf.o" "..\src\includes\xercesc\util\RefStackOf.c"
..srcincludesxercescutilRefStackOf.c:30:1: error: unknown type name 'XERCES_CPP_NAMESPACE_BEGIN'
 XERCES_CPP_NAMESPACE_BEGIN
..srcincludesxercescutilRefStackOf.c:35:10: error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token
 template <class TElem>
      ^
..srcincludesxercescutilRefStackOf.c:44:10: error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token
 template <class TElem> RefStackOf<TElem>::~RefStackOf()
..srcincludesxercescutilRefStackOf.c:...
...this and that, this and that.... and finally...
..srcincludesxercescutilRefStackOf.c:160:1: error: expected '=', ',', ';', 'asm' or '__attribute__' at end of input
 XERCES_CPP_NAMESPACE_END

如果我错误地删除了文件,错误就会弹出到另一个相同格式的文件中,开头是"我不知道XERCES_CPP_NAMESPACE_BEGIN的意思">

我也尝试过使用另一个构建器,比如mingw32-make,但它也会生成相同格式的错误。只是稍微更改一下标题,也许文件是按照不同的顺序编译的,从以下开始:

mingw32-make all
'Building file: ../src/includes/xercesc/util/BaseRefVectorOf.c'
'Invoking: GCC C Compiler'
gcc -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/includes/xercesc/util/BaseRefVectorOf.d" -MT"src/includes/xercesc/util/BaseRefVectorOf.o" -o "src/includes/xercesc/util/BaseRefVectorOf.o" "../src/includes/xercesc/util/BaseRefVectorOf.c"
../src/includes/xercesc/util/BaseRefVectorOf.c:24:1: error: unknown type name 'XERCES_CPP_NAMESPACE_BEGIN'
XERCES_CPP_NAMESPACE_BEGIN

我猜构建程序不知道如何替换

XERCES_CPP_NAMESPACE_BEGIN,带

命名空间XERCES_CPP_namespace{}
命名空间xercesc=XERCES_CPP_namespace;

但我不知道如何教建设者如何做到这一点,也不确定我是否以正确的方式编译了库。

有人能给我指个方向,告诉我如何解决这个问题吗?我只需使用cmd就可以编译一个简单的程序,所以我当然应该能够在Eclipse中完成它。

背景:

  • 操作系统:Windows 8 64位
  • 编译器:mingw-w64 5.3.0 posix-seh-rev0
  • 用msys编译的lib
  • lib编译命令:
  • /configure--prefix=/specific location--host=x86_64-w64-mingw32--启用netaccessor winsock--启用转码器窗口--禁用漂亮的make
  • make LDFLAGS=-无未定义
  • 进行检查
  • 进行安装

cmd编译命令:g++-Lib-Iinclude-o b.exe test.cpp

因此,您可以看到,我还使用-Iinclude命令将每个xerces-c头都包含在编译器中,因此我认为g++在Eclipse中调用时不会产生错误,也不知道它的gcc。

使用cmd:简单编译时运行的简单程序

//test.cpp
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/sax/HandlerBase.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <iostream>
using namespace std;
using namespace xercesc;
int main (int argc, char* args[]) {
    try {
        XMLPlatformUtils::Initialize();
    }
    catch (const XMLException& toCatch) {
        char* message = XMLString::transcode(toCatch.getMessage());
        cout << "Error during initialization! :n"
             << message << "n";
        XMLString::release(&message);
        return 1;
    }
    XercesDOMParser* parser = new XercesDOMParser();
    parser->setValidationScheme(XercesDOMParser::Val_Always);
    parser->setDoNamespaces(true);    // optional
    ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
    parser->setErrorHandler(errHandler);
    char* xmlFile = "x1.xml";
    try {
        parser->parse(xmlFile);
        DOMDocument* xmlDoc = parser->getDocument();
        DOMElement* elementRoot = xmlDoc->getDocumentElement();
        if( !elementRoot ) throw(std::runtime_error( "empty XML document" ));
        DOMNodeList* children = elementRoot->getChildNodes();
        const  XMLSize_t nodeCount = children->getLength();
        cout << nodeCount << " nodesn";
    }
    catch (const XMLException& toCatch) {
        char* message = XMLString::transcode(toCatch.getMessage());
        cout << "Exception message is: n"
             << message << "n";
        XMLString::release(&message);
        return -1;
    }
    catch (...) {
        cout << "Unexpected Exception n" ;
        return -1;
    }
    delete parser;
    delete errHandler;
    return 0;
}

@编辑

经过进一步的研究,XERCES_CPP_NAMESPACE_BEGIN似乎是在预处理器中处理的,但它只在文件util/XercesDefs.hpp 中定义

在有编译错误的文件中,它们总是以开头

#if defined(XERCES_TMPLSINC)
#include <xercesc/util/RefStackOf.hpp> //or include anything else blablabla, which ultimately leads to XercesDefs.hpp
#endif

我在整个构建目录中搜索了字符串XERCES_TMPLSINC,它包含在44.c或.hpp文件中,但每个文件都是#if !defined(XERCES_TMPLSINC)<=====错误,就像XERCES_TMPLSINC从未真正定义过一样。

根据一些论坛帖子,XERCES_TMPLSINC是一些旧的c编译器所必需的,所以有人知道如何在我的构建中解决这个问题吗?如何在项目中定义XERCES_TMPLSINC?我已经尝试将#define XERCES_TMPLSINC添加到helloworld文件中,但它仍然不起作用。

@编辑

糟糕的是,实际上所有的.c文件都包含#if defined(XERCES_TMPLSINC),所有的hpp文件都是#if !defined(XERCES_TMPLSINC),这看起来肯定是c和c++的事情吗?

通过在g++中添加以下preprocessor defs,我能够在Eclipse(在Linux上(中编译它:

-DXERCESC_INCLUDE_GUARD_WEAVEPATH_CPP -DXERCES_HAVE_STDINT_H -DXERCES_TMPLSINC.

在Windows上,我认为你应该用代替

-DXERCES_HAVE_STDINT_H -DXERCES_HAVE_CSTDINT