user187676
user187676

Reputation:

Use variadic C functions in Go

I use shm_open with cgo. shm_open is defined with 3 arguments on Linux

int shm_open(const char *name, int oflag, mode_t mode);

whereas on OSX (Darwin) the 3rd mode flag is optional.

int shm_open(const char *name, int oflag, ...);

This creates a problem with CGO when trying to pass a mode on OSX. It complains that I pass 3 arguments, when only 2 are expected.

How can I work around this?

Upvotes: 5

Views: 1193

Answers (1)

user187676
user187676

Reputation:

As usual the revelation comes 1 second after posting to SO. You can actually declare functions in the CGO comment section, so all you have to do is use a wrapper like this.

/*
#include <stdio.h>

int shm_open2(const char *name, int oflag, mode_t mode) {
  return shm_open(name, oflag, mode);
}
*/
import "C"

Upvotes: 6

Related Questions