【发布时间】:2016-08-26 07:14:56
【问题描述】:
我正在尝试在 Windows 上使用 xerces-c 进行编程。
我已经成功地构建了库并成功编译了一个简单的程序,准系统,只有 cmd 和记事本。然而,当我试图将事情转移到 Eclipse 时,事情就有点失控了。
通过简单的c++ helloworld示例,然后将包含文件包含到项目路径中并构建,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"
..\src\includes\xercesc\util\RefStackOf.c:30:1: error: unknown type name 'XERCES_CPP_NAMESPACE_BEGIN'
XERCES_CPP_NAMESPACE_BEGIN
..\src\includes\xercesc\util\RefStackOf.c:35:10: error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token
template <class TElem>
^
..\src\includes\xercesc\util\RefStackOf.c:44:10: error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token
template <class TElem> RefStackOf<TElem>::~RefStackOf()
..\src\includes\xercesc\util\RefStackOf.c:...
...this and that, this and that.... and finally...
..\src\includes\xercesc\util\RefStackOf.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 --enable-netaccessor-winsock --enable-transcoder-windows --disable-pretty-make
- 使 LDFLAGS=-no-undefined
- 检查
- 进行安装
cmd编译命令:g++ -Llib -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 << " nodes\n";
}
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)
根据forum post 的说法,一些旧的 c 编译器需要 XERCES_TMPLSINC,所以有人知道如何在我的构建中解决这个问题吗?如何在项目中定义 XERCES_TMPLSINC?我已经尝试将#define XERCES_TMPLSINC 添加到 helloworld 文件中,但它仍然不起作用。
@编辑
我的错,实际上所有的 .c 文件都包含 #if defined(XERCES_TMPLSINC) 而所有 hpp 文件都是 #if !defined(XERCES_TMPLSINC),这绝对看起来是 c 和 c++ 的东西?
【问题讨论】:
-
有趣的是这些文件有一个 .c 扩展名。它们似乎包含 C++ 模板。无论如何,Eclipse 看到 .c 并尝试使用 GCC 编译,这是一个 C 编译器,而不是 g++,需要获得那些有趣的
template好东西。没有适合您的工作,但希望这会帮助您找到一个。 -
@user4581301 谢谢你的想法。我只是尝试将 eclipse 中的 c 编译器从 gcc 替换为 g++,即使它给出了 g++ -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",它仍然给出相同的错误,我将尝试进一步调查
标签: c++ c eclipse gnu-make xerces-c