英文:
Use variadic C functions in Go
问题
我在使用cgo时使用了shm_open
函数。在Linux上,shm_open
函数有3个参数:
int shm_open(const char *name, int oflag, mode_t mode);
而在OSX(Darwin)上,第三个模式标志是可选的。
int shm_open(const char *name, int oflag, ...);
这在尝试在OSX上传递模式时会导致CGO出现问题。它会抱怨我传递了3个参数,而只期望2个参数。
我该如何解决这个问题?
英文:
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?
答案1
得分: 6
通常情况下,在发布到SO(Stack Overflow)后的1秒钟内就会有答案。实际上,你可以在CGO注释部分声明函数,所以你只需要使用这样的包装器。
/*
#include <stdio.h>
int shm_open2(const char *name, int oflag, mode_t mode) {
return shm_open(name, oflag, mode);
}
*/
import "C"
英文:
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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论