Naor Tedgi
Naor Tedgi

Reputation: 5707

building shared object - compiling with ninja works on osx and ubuntu crush on windows

I am trying to build a cld3 wrapper to JVM based language using Java Abstracted Foreign Function Layer .

I created a small class that converts the answer from the c++ library to a buffer .

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));

}

then in the JVM side :

   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)
    }

i managed to compile this code using the The Ninja build system .

and created so and dylib files :

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

everything works fine.

I am trying to create a shared object on windows with the same code I use to compile other operation systems and I get this errors:

../../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();
      ^

I already tried to pass cflags to the ninja build.

I will be happy to know how to solve this issue, and also to understand why other operations system tolerance this error.

thanks.

Upvotes: 0

Views: 121

Answers (1)

user7860670
user7860670

Reputation: 37587

long is not a suitable type to store pointer value. On 64 bit Windows long is typically only 32 bit. You should use ::std::uintptr_t which is guaranteed to be large enough instead.

Upvotes: 2

Related Questions