C library with function overloading

#1

I’m linking to a C library (OpenGL) and want to use it’s functions within Rust. The problem is that the library uses function overloading, and I can’t get Rust to accept that.

I can’t setup a playground for this, but basically I just want this to compile:

#[link(name = "opengl32")]
extern "C" {
    pub fn glEnableVertexAttribArray(index: u32);
    pub fn glEnableVertexAttribArray(index: u32, vaobj: u32);
}

This gives me back

error[E0428]: the name `glEnableVertexAttribArray` is defined multiple times
  --> src\main.rs:74:5
   |
73 |     pub fn glEnableVertexAttribArray(index: u32);
   |     --------------------------------------------- previous definition of the value `glEnableVertexAttribArray` here
74 |     pub fn glEnableVertexAttribArray(index: u32, vaobj: u32);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `glEnableVertexAttribArray` redefined here
   |
   = note: `glEnableVertexAttribArray` must be defined only once in the value namespace of this module

error: aborting due to previous error

What is the best way around this?

#2

C doesn’t have function overloading. Could this be C++, or varargs or some macro trick?

You can convert C headers with bindgen. It should handle many non-trivial cases.

There are also existing OpenGL wrapper libraries for Rust already. Presumably they have figured this case out.

1 Like
#3

Google says one is attrib array, while the other is array attrib.

1 Like
#4

Wow I must be dyslexic, thanks!

#5

No, you are not. I only realized it because I asked my son to read them out loud. Since he don’t speaks English, doesn’t program and is in his first year of school, he really read was what there, not what he expected to see :wink:

6 Likes