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

Create a tutorial for working with C libraries #68

Open
joshring opened this issue Oct 6, 2024 · 1 comment
Open

Create a tutorial for working with C libraries #68

joshring opened this issue Oct 6, 2024 · 1 comment

Comments

@joshring
Copy link
Contributor

joshring commented Oct 6, 2024

This is more of a brain dump each of these are likely an issue

  • C header only libraries - usually do an example with addition or similar
  • Larger C libraries, writing C3 bindings make sure to link to C interop's gotchas!
  • Working with Static C libraries
  • Tips for porting C libraries
@tomaskallup
Copy link
Contributor

It would be nice to also include a section about working with c-sources in project.

I was able to make a quick example, storing it here for reference.
In project folder, I created c-src folder, to put any C libraries in there.

.
└── c-src
    └── mylib
        ├── include
        │   └── mylib.h
        └── src
            └── mylib.c

mylib.c has and include for the header #include "mylib.h"

This is then glued together via project.json options:

{
  "c-sources": [ "./c-src/mylib/src/" ],
  "c-include-dirs": [ "./c-src/mylib/include" ],
}

When running c3c build these options are just passed to cc:

cc -I ./c-src/mylib/include/ -fPIE -fPIC -c ./c-src/mylib/src/*.c -o build/tmp/tmp_c_compile/mylib.o

And the result is just linked to the C3 code. This means that any functions which we want to use from mylib need to be declared in C3:

extern fn void do_stuff(int val);

fn int main(String[] args)
{
  do_stuff(0);
  return 0;
}

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

No branches or pull requests

2 participants