duanhan5230 2017-05-16 12:37
浏览 53

使用Go构建的C ++ xerces程序编译错误

I try to embed a little c++ xerces program in my go application in order to validate xml against xsd. The program compiles and runs fine when I use g++ with the following command:

g++ test-xerces.cpp -o xercesTest -v -lxerces-c ./xercesTest

However, when using go build, I get the following output:

go build validation-xerces.go 
    # command-line-arguments
    In file included from 
    /usr/local/include/xercesc/util/RefHashTableOf.hpp:252:0,
                 from 
    /usr/local/include/xercesc/internal/XSerializeEngine.hpp:25,
                 from 
    /usr/local/include/xercesc/internal/XSerializable.hpp:25,
                 from /usr/local/include/xercesc/util/QName.hpp:30,
                 from /usr/local/include/xercesc/framework/XMLAttr.hpp:26,
                 from 
    /usr/local/include/xercesc/framework/XMLDocumentHandler.hpp:27,
                 from 
    /usr/local/include/xercesc/parsers/AbstractDOMParser.hpp:26,
                 from 
    /usr/local/include/xercesc/parsers/XercesDOMParser.hpp:26,
                 from ./validation-xerces.go:10:
    /usr/local/include/xercesc/util/RefHashTableOf.c:33:15: fatal error: new: No such file or directory
    #include <new>
               ^
compilation terminated.

The program in question (I've stolen almost every part of it from people smarter than me):

package main
/*
#cgo LDFLAGS: lxerces-c

#include <stdio.h>

#include <xercesc/util/XMLString.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/framework/LocalFileInputSource.hpp>
#include <xercesc/sax/ErrorHandler.hpp>
#include <xercesc/sax/SAXParseException.hpp>
#include <xercesc/sax/EntityResolver.hpp>

XERCES_CPP_NAMESPACE_USE

class ParserErrorHandler : public ErrorHandler
{
private:
    void reportParseException(const SAXParseException& ex)
    {
        char* msg = XMLString::transcode(ex.getMessage());
        fprintf(stderr, "at line %llu column %llu, %s
",
                ex.getColumnNumber(), ex.getLineNumber(), msg);
        XMLString::release(&msg);
    }

public:
    void warning(const SAXParseException& ex)
    {
        reportParseException(ex);
    }

    void error(const SAXParseException& ex)
    {
        reportParseException(ex);
    }

    void fatalError(const SAXParseException& ex)
    {
        reportParseException(ex);
    }

    void resetErrors()
    {
    }
};

void ValidateSchema(const char* xmlFilePath)
{
    XercesDOMParser domParser;

    ParserErrorHandler parserErrorHandler;
    domParser.setErrorHandler(&parserErrorHandler);
    domParser.setValidationScheme(XercesDOMParser::Val_Auto);
    domParser.setDoNamespaces(true);
    domParser.setDoSchema(true);
    domParser.setValidationConstraintFatal(true);

    domParser.parse(xmlFilePath);
    if (domParser.getErrorCount() == 0)
        printf("XML file validated against the schema successfully
");
    else
        printf("XML file doesn't conform to the schema
");
}

int Cmain(int argc, const char *argv[])
{
    if (argc != 2)
    {
        printf("SchemaValidator <xml file>
");
        return 0;
    }

    XMLPlatformUtils::Initialize();

    ValidateSchema(argv[1]);

    XMLPlatformUtils::Terminate();

    return 0;
}
*/
import "C"
import (
    "unsafe"
)

func main() {
    argv := [][]byte{
        []byte(`/path/to/my/file.xml`),
    }
    c_argc := C.int(len(argv))

    var b *C.char
    ptrSize := unsafe.Sizeof(b)
    c_argv := C.malloc(C.size_t(len(argv)) * C.size_t(ptrSize))
    defer C.free(unsafe.Pointer(c_argv))

    // Assign each byte slice to its appropriate offset.
    for i := 0; i < len(argv); i++ {
        element := (**C.char)(unsafe.Pointer(uintptr(c_argv) + uintptr(i)*ptrSize))
        *element = (*C.char)(unsafe.Pointer(&argv[i][0]))
    }

    C.Cmain(c_argc, (**C.char)(c_argv))
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂