Skip to content
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

Add C FFI #171

Merged
merged 9 commits into from
Aug 18, 2023
Merged

Add C FFI #171

merged 9 commits into from
Aug 18, 2023

Conversation

mattxwang
Copy link
Member

@mattxwang mattxwang commented Aug 3, 2023

Working C FFI! Here's the most basic possible example. After installing cbindgen,

cbindgen --config cbindgen.toml --crate rsdd --output rsdd.h

to generate rsdd.h. Then, create a .c file:

#include "rsdd.h"
#include <stdio.h>

char* cnf_string =
"p cnf 6 3\n"
"1 2 3 4 0\n"
"-2 -3 4 5 0\n"
"-4 -5 6 6 0\n";

int main() {
  VarOrder* order = var_order_linear(6);
  Cnf* cnf = cnf_from_dimacs(cnf_string);
  RsddBddBuilder* builder = robdd_builder_all_table(order);
  BddPtr* bdd = robdd_builder_compile_cnf(builder, cnf);
  uint64_t mc = robdd_model_count(builder, bdd);
  printf("Model Count: %llu\n", mc);
}

build RSDD:

cargo build

then, build the C file and run it!

$  gcc main.c -o main -lrsdd -L./target/debug
$ ./main
Model Count: 48

nice ™️

@mattxwang mattxwang marked this pull request as draft August 3, 2023 19:51
@mattxwang mattxwang changed the title Add C interop Add C FFI Aug 18, 2023
@mattxwang mattxwang marked this pull request as ready for review August 18, 2023 15:24
@mattxwang mattxwang merged commit d85e55b into main Aug 18, 2023
6 of 7 checks passed
@mattxwang mattxwang deleted the re-add-ffi branch August 18, 2023 15:24
mattxwang added a commit that referenced this pull request Sep 13, 2023
Essentially codifies #171 into some CI; this is necessary so that we don't have duplicate symbols with rsdd-ocaml.

(the instructions for #171 still work, *except* you now need to build with the feature flag, i.e. `cargo build --features="ffi"`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant