Program crash upon linking to C++ library with a C interface


#1

Hi! I’m currently trying to write a rust FFI that links into the C++ VTFLib library (which exposes a C interface). I’ve started writing the FFI so that it calls some basic functions from the library to get the version number, initialize the library, and shut down the library. Whenever I try to call any of the functions the program compiles, then crashes with an exit code of -1073741819. To set up the library, I downloaded the binary archive and placed the files “VTFLib.dll” and “HLLib.dll” from the archive’s \bin\x64 folder into my \Rust stable 1.5\bin\rustlib\x86_64-pc-windows-gnu\lib folder as well as into my cargo project’s \target\debug folder. I’m referencing the header file \lib\VTFLib.h from the binary archive (which you can view here).

This is the program that I’m running:

extern crate vtflib;

fn main() {
    unsafe {
        vtflib::ffi::vlInitialize(); //From VTFLib.h line 450
        vtflib::ffi::vlShutdown(); //From VTFLib.h line 451
        println!("OK!");
    }
}

This is the FFI:

use libc::{c_uchar, c_char, c_short, c_ushort, c_int, c_uint, c_long, c_ulong, c_float, c_double, c_void};

//Custom data types

///Boolean value 0/1
pub type vl_bool = c_uchar;
///Single signed character
pub type vl_char = c_char;
///Single unsigned byte
pub type vl_byte = c_uchar;
///Signed short floating point value
pub type vl_short = c_short;
///Unsigned short floating point value
pub type vl_ushort = c_ushort;
///Signed integer value
pub type vl_int = c_int;
///Unsigned integer value
pub type vl_uint = c_uint;
///Signed long number
pub type vl_long = c_long;
///Unsigned long number
pub type vl_ulong = c_ulong;
///Floating point number
pub type vl_single = c_float;
///Double number
pub type vl_double = c_double;
///Void value
pub type vl_void = c_void;

#[link(name = "VTFLib")]
extern "C" {
    pub fn vlGetVersion() -> vl_uint; //From VTFLib.h line 445
    pub fn vlInitialize() -> vl_bool; //From VTFLib.h line 450
    pub fn vlShutdown() -> vl_void; //From VTFLib.h line 451
}

Any advice on getting it working? Thanks in advance.


#2

c_void is actually an enum, and it’s only intended for use as *mut c_void to represent a C void*. To indicate that a function doesn’t return anything, use the return type (), or simply leave off the ->.

That would only affect vlShutdown, so it may not be your only problem if all the functions are crashing.