cargo-c icon indicating copy to clipboard operation
cargo-c copied to clipboard

Document that the capi feature must be present in the Cargo.toml

Open mgeier opened this issue 4 years ago • 7 comments

If my Cargo.toml does not contain a [features] section defining the capi feature, the header file is generated correctly, but the dynamic library does not contain the expected symbols.

The README says:

You may use the feature capi to add C-API-specific optional dependencies.

... but it doesn't say that defining this feature is required.

UPDATE: I'm trying this on Linux, btw.

mgeier avatar Dec 14 '20 13:12 mgeier

Do you have your code available so I can take a look?

lu-zero avatar Dec 14 '20 17:12 lu-zero

I've created #153 as an example.

The error appears in CI.

mgeier avatar Dec 14 '20 19:12 mgeier

It works as intended.

cargo rejects setting features that aren't present in the manifest. I cannot work around this limitation without changing the cargo API.

lu-zero avatar Dec 15 '20 07:12 lu-zero

This can be quite confusing, because the header contains all the functions, but the library doesn't. Would it at least be possible to have the header file and the library contain (or not contain) the same functions?

The instruction "... use #[cfg(feature="capi")] to hide it when you build a normal rust library" from the README should not be separated from the instruction "You may use the feature capi to add C-API-specific optional dependencies.", because those two are not independent.

Still, even with updated documentation, this might cause a lot of confusion in the future, it would be great if it were possible to avoid the problem completely ...

What was the reason for deprecating #[cfg(cargo_c)]? Using this would avoid the problem.

mgeier avatar Dec 16 '20 09:12 mgeier

there are two issues:

  • setting cfg(cargo_c) works but not for the tests because of how cargo rustc works, relaxing this constraint requires patching cargo.
  • adding on the fly features to the manifest requires patching cargo to add a setter for it.

On top cbindgen is happy to ignore cfg(feature="capi").

Possibly I can send patches to upstream cargo to get those two sorted.

lu-zero avatar Dec 16 '20 10:12 lu-zero

In the meantime, the README contains the information that the capi feature is obligatory.

But maybe it would be helpful to explicitly mention how to do that?

Something like this:

[features]
capi = []

mgeier avatar Jan 10 '22 19:01 mgeier

It sounds a good idea indeed. :)

lu-zero avatar Jan 11 '22 09:01 lu-zero