构建共享对象-在Windows的osx和ubuntu上使用ninja进行编译

时间:2019-06-09 07:03:24

标签: c++ kotlin java-native-interface chromium ninja

我正在尝试使用以下语言为基于JVM的语言构建cld3包装器  Java Abstracted Foreign Function Layer

我创建了一个小类,将答案从c ++库转换为缓冲区。

void detect(long ptr, const char *text, const char *into) {

    NNetLanguageIdentifier *nptr = (NNetLanguageIdentifier *) ptr;
    NNetLanguageIdentifier::Result res = nptr->FindLanguage(text);

    long current = (long) into;

    *((float *) current) = res.probability;
    current += sizeof(float);
    *((float *) current) = res.proportion;
    current += sizeof(float);
    *((short *) current) = res.is_reliable;
    current += sizeof(short);
    *((int *) current) = res.language.size();
    current += sizeof(int);

    memccpy(reinterpret_cast<void *>(current), res.language.c_str(), res.language.size() + 1, sizeof(res.language));

}

然后在JVM端:

   fun detect(text: String): LangDetectResponse {
        val buffer = ByteArray(SINGLE_LANGUAGE_DETECTION_BUFFER_SIZE)
        detector.detect(ptr, text, buffer)

        ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).let {
            return extractResponseFromByteBuffer(it)
        }
    }

    private fun extractResponseFromByteBuffer(byteBuffer: ByteBuffer): LangDetectResponse {
        val probability = byteBuffer.float
        val proportion = byteBuffer.float
        val isReliable = byteBuffer.short
        val sizeOfLanguage = byteBuffer.int
        val languageBuffer = ByteArray(sizeOfLanguage) { 127.toByte() }
        byteBuffer.get(languageBuffer, 0, sizeOfLanguage)
        val language = String(languageBuffer, StandardCharsets.UTF_8)
        return LangDetectResponse(probability, proportion, isReliable.toInt() == 1, language)
    }

我设法使用The Ninja build system 编译了这段代码。

创建了so和dylib文件:

https://github.com/ntedgi/cld3-kotlin#operations-systems-support

一切正常。

我试图使用与编译其他操作系统相同的代码在 windows 上创建共享库,但出现以下错误:

../../third_party/cld_3/src/src/Cld3LangDetector.cc(19,36): error: cast to 'chrome_lang_id::NNetLanguageIdentifier *' from smaller integer type 'long' [-Werror,-Wint-to-pointer-cast]
    NNetLanguageIdentifier *nptr = (NNetLanguageIdentifier *) ptr;
                                   ^
../../third_party/cld_3/src/src/Cld3LangDetector.cc(25,36): error: cast to 'chrome_lang_id::NNetLanguageIdentifier *' from smaller integer type 'long' [-Werror,-Wint-to-pointer-cast]
    NNetLanguageIdentifier *nptr = (NNetLanguageIdentifier *) ptr;
                                   ^
../../third_party/cld_3/src/src/Cld3LangDetector.cc(31,7): error: cast to 'float *' from smaller integer type 'long' [-Werror,-Wint-to-pointer-cast]
    *((float *) current) = res.probability;
      ^
../../third_party/cld_3/src/src/Cld3LangDetector.cc(33,7): error: cast to 'float *' from smaller integer type 'long' [-Werror,-Wint-to-pointer-cast]
    *((float *) current) = res.proportion;
      ^
../../third_party/cld_3/src/src/Cld3LangDetector.cc(35,7): error: cast to 'short *' from smaller integer type 'long' [-Werror,-Wint-to-pointer-cast]
    *((short *) current) = res.is_reliable;
      ^
../../third_party/cld_3/src/src/Cld3LangDetector.cc(37,7): error: cast to 'int *' from smaller integer type 'long' [-Werror,-Wint-to-pointer-cast]
    *((int *) current) = res.language.size();
      ^

我已经尝试过将标志传递给忍者版本。

我将很高兴知道如何解决此问题,并了解为什么其他操作系统会容忍此错误。

谢谢。

1 个答案:

答案 0 :(得分:2)

long不适合用于存储指针值。在64位Windows long上通常仅为32位。您应该使用::std::uintptr_t来保证它足够大。