https://kotlinlang.org logo
#kotlin-native
Title
# kotlin-native
o

Omico

03/14/2023, 9:48 AM
I'm trying to use tdlib in Kotlin Native. Anything wrong with my code?
Copy code
kotlin {
    macosX64()
    linuxX64()
    mingwX64()
    targets.withType<KotlinNativeTarget> {
        compilations["main"].cinterops {
            create("tdlib")
        }
    }
}
Copy code
linkerOpts = \
    -ltdactor \
    -ltdapi \
    -ltdclient \
    -ltdcore \
    -ltddb \
    -ltdjson \
    -ltdjson_private \
    -ltdjson_static \
    -ltdnet \
    -ltdsqlite \
    -ltdutils

linkerOpts.mingw = \
    -I"D:\Git\Omico\telegram-bot\TDLib\tdlib\include" \
    -L"D:\Git\Omico\telegram-bot\TDLib\tdlib\lib"
v

vbsteven

03/14/2023, 10:15 AM
Seems to be OK, are you experiencing issues with something?
o

Omico

03/14/2023, 10:30 AM
@vbsteven The lib seems to have failed to import. Nothing here.
Also here is the tdlib install path
v

vbsteven

03/14/2023, 10:42 AM
Can you show the full
.def
file? if nothing shows up in the klib you are likely missing something in the
headers
and
headerFilter
properties from the def file
Typically you would put the toplevel headers in
headers = "mylib.h"
and use
headerFilter = "something/*"
to perform additional filtering. Then cinterop will try to "walk" everything in
headers
recursively using includes, and every type/function/constant it finds in files that match
headerFilter
will show up in the klib.
o

Omico

03/14/2023, 11:23 AM
I have provided the full
tdlib.def
file before. And here is the full install path, I have no idea how to use
headerFilter
Copy code
.
├── bin
│   ├── libcrypto-3-x64.dll
│   ├── libssl-3-x64.dll
│   ├── tdjson.dll
│   └── zlib1.dll
├── include
│   └── td
│       ├── telegram
│       │   ├── Client.h
│       │   ├── Log.h
│       │   ├── td_api.h
│       │   ├── td_api.hpp
│       │   ├── td_json_client.h
│       │   ├── tdjson_export.h
│       │   └── td_log.h
│       └── tl
│           └── TlObject.h
└── lib
    ├── cmake
    │   └── Td
    │       ├── TdConfig.cmake
    │       ├── TdConfigVersion.cmake
    │       ├── TdTargets.cmake
    │       └── TdTargets-release.cmake
    ├── pkgconfig
    │   ├── tdactor.pc
    │   ├── tdapi.pc
    │   ├── tdclient.pc
    │   ├── tdcore.pc
    │   ├── tddb.pc
    │   ├── tdjson.pc
    │   ├── tdjson_private.pc
    │   ├── tdjson_static.pc
    │   ├── tdnet.pc
    │   ├── tdsqlite.pc
    │   └── tdutils.pc
    ├── tdactor.lib
    ├── tdapi.lib
    ├── tdclient.lib
    ├── tdcore.lib
    ├── tddb.lib
    ├── tdjson.lib
    ├── tdjson_private.lib
    ├── tdjson_static.lib
    ├── tdnet.lib
    ├── tdsqlite.lib
    └── tdutils.lib
v

vbsteven

03/14/2023, 11:48 AM
Copy code
headers = "td/telegram/Client.h" "td/tl/TlObject.h" // and probably other headerfiles if not everything is included by Client.h
Copy code
headerFilter = "td/telegram/*" "td/tl/*"

compilerOpts.mingw64 = I"D:\Git\Omico\telegram-bot\TDLib\tdlib\include"
something like that should get you started
o

Omico

03/14/2023, 12:10 PM
My current full
tdlib.def
is:
Copy code
headers = \
    "td/telegram/Client.h" \
    "td/tl/TlObject.h"

compilerOpts.mingw = \
    -I"D:\Git\Omico\telegram-bot\TDLib\tdlib\include"

linkerOpts = \
    -ltdactor \
    -ltdapi \
    -ltdclient \
    -ltdcore \
    -ltddb \
    -ltdjson \
    -ltdjson_private \
    -ltdjson_static \
    -ltdnet \
    -ltdsqlite \
    -ltdutils

linkerOpts.mingw = \
    -L"D:\Git\Omico\telegram-bot\TDLib\tdlib\lib"
It shows an error
Copy code
Exception in thread "main" java.lang.Error: C:\Users\Omico\AppData\Local\Temp\1632501521883779092.c:1:10: fatal error: 'td/telegram/Client.h' file not found
	at org.jetbrains.kotlin.native.interop.indexer.UtilsKt.ensureNoCompileErrors(Utils.kt:274)
	at org.jetbrains.kotlin.native.interop.indexer.IndexerKt.indexDeclarations(Indexer.kt:1196)
	at org.jetbrains.kotlin.native.interop.indexer.IndexerKt.buildNativeIndexImpl(Indexer.kt:1185)
	at org.jetbrains.kotlin.native.interop.indexer.IndexerKt.buildNativeIndexImpl(Indexer.kt:1181)
	at org.jetbrains.kotlin.native.interop.gen.jvm.DefaultPlugin.buildNativeIndex(Plugins.kt:33)
	at org.jetbrains.kotlin.native.interop.gen.jvm.MainKt.processCLib(main.kt:294)
	at org.jetbrains.kotlin.native.interop.gen.jvm.MainKt.processCLibSafe(main.kt:219)
	at org.jetbrains.kotlin.native.interop.gen.jvm.MainKt.interop(main.kt:80)
	at org.jetbrains.kotlin.cli.utilities.InteropCompilerKt.invokeInterop(InteropCompiler.kt:45)
	at org.jetbrains.kotlin.cli.utilities.MainKt.mainImpl(main.kt:40)
	at org.jetbrains.kotlin.cli.utilities.MainKt.main(main.kt:62)

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':core:cinteropTdlibLinuxX64'.
> Process 'command 'C:\Program Files\Eclipse Adoptium\jdk-19.0.2.7-hotspot\bin\java.exe'' finished with non-zero exit value 1
v

vbsteven

03/14/2023, 12:12 PM
It fails because it cannot find the Client.h file, you'll have to experiment a bit with the compilerOpts to make sure it is looking in the right directories. I think you can also add something like
-v
to the compilerOpts to make it print out the directories it is looking in. (but I'm not sure if it is exactly
-v
from memory)
m

msink

03/14/2023, 12:35 PM
C++ lib? Cinterop supports only pure C (and Objective-C for apple targets)
o

Omico

03/14/2023, 1:15 PM
@vbsteven I discovered that the above problem occurs because
compilerOpts.mingw
cannot be recognized correctly. Current full
tdlib.def
is:
Copy code
language = C++

headers = \
    "td/telegram/Client.h" \
    "td/tl/TlObject.h"

headerFilter = \
    "td/telegram/*" \
    "td/tl/*"

compilerOpts = \
    -v \
    -I"D:\Git\Omico\telegram-bot\TDLib\tdlib\include"

linkerOpts = \
    -L"D:\Git\Omico\telegram-bot\TDLib\tdlib\lib" \
    -ltdactor \
    -ltdapi \
    -ltdclient \
    -ltdcore \
    -ltddb \
    -ltdjson \
    -ltdjson_private \
    -ltdjson_static \
    -ltdnet \
    -ltdsqlite \
    -ltdutils
@msink Yes, it is C++ lib. I have added
language = C++
to avoid error output, but still, nothing generated. Seem there is nothing we can do now. Thanks for helping!
l

Landry Norris

03/14/2023, 1:16 PM
There's minimal C++ support. They added it to make skia integration easier, but haven't said much, since it's only some features.
I found some documentation once explaining exactly what's supported and not, but don't remember where off the top of my head.
o

Omico

03/14/2023, 1:21 PM
@Landry Norris Yes! I think we are in the same boat.
l

Landry Norris

03/14/2023, 1:23 PM
Either way, normal cinterop should see all extern C methods. If it doesn't, there's something wrong with the def file.
91 Views