cbindgen icon indicating copy to clipboard operation
cbindgen copied to clipboard

Complex number support

Open fstop22 opened this issue 10 months ago • 2 comments

I am using cbindgen v0.28.0 to create bindings to Rust functions that have complex arguments. For example,

#[no_mangle]
pub extern "C" fn channelizer_analysis_bank_2x(ptr: *mut Channelizer,
                                               size: u32,
                                               in_ptr: *const Complex32,
                                               out_ptr: *mut Complex32) -> usize

cbindgen generates the following header file.

#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <ostream>
#include <new>

struct Channelizer;

using Complex32 = Complex<float>;

extern "C" {

uintptr_t channelizer_analysis_bank_2x(Channelizer *ptr,
                                       uint32_t size,
                                       const Complex32 *in_ptr,
                                       Complex32 *out_ptr);

g++ produces the following errors when trying to compile.

In file included from ./apps/chan_test.c:5:
../rust/lib/rust_channelizer/channelizer/rust_chan_bindings.h:9:19: error: ‘Complex’ does not name a type
    9 | using Complex32 = Complex<float>;
      |                   ^~~~~~~
../rust/lib/rust_channelizer/channelizer/rust_chan_bindings.h:22:46: error: ‘Complex32’ does not name a type
   22 |                                        const Complex32 *in_ptr,
      |                                              ^~~~~~~~~
../rust/lib/rust_channelizer/channelizer/rust_chan_bindings.h:23:40: error: ‘Complex32’ has not been declared
   23 |                                        Complex32 *out_ptr);
      |                                        ^~~~~~~~~

It appears the the generated header file has the following errors:

  1. Missing #include <complex>
  2. using Complex32 = Complex<float>; should be using Complex32 = complex<float>;

Applying these changes to the generate header file, I can successfully compile.

fstop22 avatar Mar 05 '25 21:03 fstop22