-
Notifications
You must be signed in to change notification settings - Fork 67
LibTorrent bindings #218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
clang can't find those headers because it needs an extra include directory. In ffigen.yaml, use the compiler-opts:
- '-Isrc2/include/' |
Did you solve this, Davenchy? If yes - can you share your configuration for ffigen for libtorrent? |
Hey @Kirill-21 the answer helped to generate code but I still can't use the generated code because it doesn't contain the actual classes and methods found in libtorrent docs also the generated code has errors for undeclared types This is my # Run with `dart run ffigen --config ffigen.yaml`.
name: DartLibTorrentBindings
description: |
Bindings for `LibTorrent`.
Regenerate bindings with `dart run ffigen --config ffigen.yaml`.
output: "lib/bindings_generated.dart"
headers:
entry-points:
- "src2/include/libtorrent/libtorrent.hpp"
compiler-opts:
- '-Isrc2/include'
preamble: |
// ignore_for_file: always_specify_types
// ignore_for_file: camel_case_types
// ignore_for_file: non_constant_identifier_names
comments:
style: any
length: full |
@Davenchy I'm assuming you used https://github.com/arvidn/libtorrent/blob/RC_2_0/include/libtorrent/libtorrent.hpp ? As far as I can see this is C++ not C.
_Originally posted by @dcharkes in #244 I see a C header in https://github.com/arvidn/libtorrent/blob/RC_2_0/bindings/c/libtorrent.h. But it looks like that is a rather small subset of the API. (Also, |
Hey @Davenchy, Did you end up with something working in terms of libtorrent bindings? I'm also working on libtorrent bindings for Dart / Flutter and would appreciate any lead you have 🙌 |
Hey @Kylart, Unfortunately I don't have that much experience to deal with libtorrent and writing bindings to Dart. |
There is not alternative to this day using pure dart solutions so you'd have to build your own. If I had to do it now, I would start by building pure C bindings for libtorrent that I would then use from Dart just like @dcharkes mentioned. Depending on your needs though, I would say that you can take a look at the dtorrent_task package. |
yes, but how did you do it? not to snoop but out of curiosity i checked your profile that you had build an anime streaming app using torrents, also i did check out dtorrent, it doesn't have concurrency and hence was giving very slow results |
mind i ask how I could make pure C bindings from libtorrent as it is written majorly in C++ |
@tanishbajaj101 |
would i need to do this only for the header files/functions that I use, or basically on every file (that those header files may further depend on) |
Here is a ChatGPT answer: To use C++ classes and functions in Dart, you can create C functions (wrapper functions) for the specific classes and functions you want to use. These C functions act as an interface between your Dart code and the C++ code. Here's a basic example of how you can do it:
// my_class.hpp
#pragma once
class MyClass {
public:
MyClass();
int add(int a, int b);
}; // my_class.cpp
#include "my_class.hpp"
MyClass::MyClass() {}
int MyClass::add(int a, int b) {
return a + b;
}
// my_class_c_wrapper.h
#ifdef __cplusplus
extern "C" {
#endif
typedef void* MyClassHandle;
MyClassHandle createMyClass();
int add(MyClassHandle handle, int a, int b);
void destroyMyClass(MyClassHandle handle);
#ifdef __cplusplus
}
#endif
// my_class_c_wrapper.cpp
#include "my_class_c_wrapper.h"
#include "my_class.hpp"
extern "C" {
MyClassHandle createMyClass() {
return reinterpret_cast<MyClassHandle>(new MyClass());
}
int add(MyClassHandle handle, int a, int b) {
MyClass* instance = reinterpret_cast<MyClass*>(handle);
return instance->add(a, b);
}
void destroyMyClass(MyClassHandle handle) {
MyClass* instance = reinterpret_cast<MyClass*>(handle);
delete instance;
}
}
With this setup, you can use the C functions ( |
My friends @tanishbajaj101, @Kirill-21, @Kylart , I've created this example project. I hope it proves useful to you. |
thanks a lot for the example project! I just wanted to request you to share the intermediate C file or header file which you might have created to generate the wrapper.cpp file for a better understanding. |
Hello there, I didn't generate the wrapper.cpp/.hpp files; instead, I crafted them from scratch to suit the project's specific requirements. Essentially, what I've done is create the The Within the header file By utilizing the provided Makefile to build the Now, you can utilize the compiled library with Dart's FFI to invoke the I acknowledge that my C++ skills may not be top-notch, and my code may not adhere to the best practices. However, my intention was to provide a functional example. I'm still learning, and this might be my first real project, where I delved into documentation and utilized a library like Regarding the casting, I've employed C-style casting to cast For further insights into casting, you can explore the following links:
Feel free to reach out if you have any further questions or need clarification on anything. Yep, you got it! I'm relying on ChatGPT to jazz up my reply because, let's face it, my English is more of a mess than my C++ code! 😅😇 |
Uh oh!
There was an error while loading. Please reload this page.
I am trying to bind libtorrent using ffigen
my
ffigen.yaml
:the output of
dart run ffigen --config ffigen.yaml
is:the
libtorrent.hpp
file is something like thisI was following this tutorial:
https://blog.logrocket.com/dart-ffi-native-libraries-flutter/
The text was updated successfully, but these errors were encountered: