构建共享对象 - 使用 ninja 编译在 osx 上工作,ubuntu crush 在 windows 上工作
building shared object - compiling with ninja works on osx and ubuntu crush on windows
我正在尝试为基于 JVM 的语言构建一个 cld3 包装器,使用
Java Abstracted Foreign Function Layer
.
我创建了一个小的 class 将答案从 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 构建系统编译了这段代码
.
并创建了 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();
^
我已经尝试将 cflags 传递给忍者版本。
我很高兴知道如何解决这个问题,也很高兴了解为什么其他操作系统容忍这个错误。
谢谢。
long
不是存储指针值的合适类型。在 64 位上 Windows long
通常只有 32 位。您应该使用保证足够大的 ::std::uintptr_t
。
我正在尝试为基于 JVM 的语言构建一个 cld3 包装器,使用 Java Abstracted Foreign Function Layer .
我创建了一个小的 class 将答案从 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 构建系统编译了这段代码 .
并创建了 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();
^
我已经尝试将 cflags 传递给忍者版本。
我很高兴知道如何解决这个问题,也很高兴了解为什么其他操作系统容忍这个错误。
谢谢。
long
不是存储指针值的合适类型。在 64 位上 Windows long
通常只有 32 位。您应该使用保证足够大的 ::std::uintptr_t
。