Thread overview
How to correctly integrate D library to Swift/Obj-C mobile project?
Jun 22
Anton
Jun 22
Kagamin
Jun 22
Vlad
Jun 22
kinke
Jun 24
Vlad
June 22
I have a static library (.a) compiled with LDC for iOS platform. But I can't figure out how to correctly connect it to the project and call its functions. I've already linked binary with library to the project but IDE still doesn't see its classes and methods. Do I need to do some additional configurations?
June 22
If you want to use them from D, you need those classes and methods declared in the D language, in text.
June 22
On Monday, 22 June 2020 at 18:40:21 UTC, Kagamin wrote:
> If you want to use them from D, you need those classes and methods declared in the D language, in text.

We want to use compiled D as a library in a iOS swift project.

Usually, when you connect c++/c, you have header files so you can call functions from Objective-C/swift code. We need something similar.

—

Is it even possible to compile D for iOS and use it the same way as compiled C++ static library? (We do need a D runtime)
June 22
On Monday, 22 June 2020 at 19:41:22 UTC, Vlad wrote:
> Usually, when you connect c++/c, you have header files so you can call functions from Objective-C/swift code. We need something similar.

There's a pretty recent -HC switch to generate C++ headers from `extern(C++)` declarations. Not sure how usable it is at this point.

> Is it even possible to compile D for iOS and use it the same way as compiled C++ static library? (We do need a D runtime)

iOS shouldn't be any different than other targets (especially macOS) in this regard.
June 24
On Monday, 22 June 2020 at 19:41:22 UTC, Vlad wrote:

> Is it even possible to compile D for iOS and use it the same way as compiled C++ static library? (We do need a D runtime)

Yes, druntime/Phobos will need to be linked like any other static library.

--
/Jacob Carlborg

June 24
On Monday, 22 June 2020 at 14:32:21 UTC, Anton wrote:
> I have a static library (.a) compiled with LDC for iOS platform. But I can't figure out how to correctly connect it to the project and call its functions. I've already linked binary with library to the project but IDE still doesn't see its classes and methods. Do I need to do some additional configurations?

When you say "IDE", I'm going to assume you're referring to Xcode. Xcode has absolutely no support for D at all.

Since the Objective-C support in LDC is so limited I would recommend creating `extern(C)` functions in the D code, which forwards to your D functions (or have the functions being `extern(C)` to begin with). Then you need to have these declarations available in a C header file, either created by using the `-HC` flag or by manually writing the declarations.

--
/Jacob Carlborg
June 24
On Wednesday, 24 June 2020 at 12:00:04 UTC, Jacob Carlborg wrote:
> On Monday, 22 June 2020 at 14:32:21 UTC, Anton wrote:
>> I have a static library (.a) compiled with LDC for iOS platform. But I can't figure out how to correctly connect it to the project and call its functions. I've already linked binary with library to the project but IDE still doesn't see its classes and methods. Do I need to do some additional configurations?
>
> When you say "IDE", I'm going to assume you're referring to Xcode. Xcode has absolutely no support for D at all.
>
> Since the Objective-C support in LDC is so limited I would recommend creating `extern(C)` functions in the D code, which forwards to your D functions (or have the functions being `extern(C)` to begin with). Then you need to have these declarations available in a C header file, either created by using the `-HC` flag or by manually writing the declarations.
>
> --
> /Jacob Carlborg

Thank you! We did it as you said and it works.

And thanks a lot for the iOS support, it's amazing!



Few notes on our finding, maybe it will help someone.

--- About header files:

We had to use DMD to create .h files. Couldn't find a way to get them via ldc, which seems to only generate .di files (probably just missing something).

In the end, we are using the compiled D library from Flutter Dart, writing pointers to symbols manually.


--- About compiling for iOS simulator:

When compiling druntime and phobos for iOS simulator, you need to add -mios-simulator-version-min=12.0 flag to --cFlags, here is an example:

ldc-build-runtime --ninja --cFlags="-mios-simulator-version-min=12.0;-target;x86_64-apple-darwin" --dFlags="-mtriple=x86_64-apple-darwin;-fvisibility=hidden" CMAKE_SYSTEM_NAME=Darwin BUILD_SHARED_LIBS=OFF

(More info: https://wiki.dlang.org/Building_LDC_runtime_libraries)

And you have to link phobos and druntime static libraries (.a) files to Xcode, not just your library.