标签:sign cgo 实现 code double ++ highlight complex import
C作为一种混合编程语言已经很久了,无论那些广泛使用的包是用何种语言实现的,都导出了和C兼容的API。Go程序调用C程序,可以借助两种工具实现,一种是cgo,另一种是SWIG工具。Cgo是为C函数创建go绑定的工具;SWIG工具可以提供更复杂的特性来调用C++程序。(关于SWIG工具的使用,可以参考以前的《calling c++ from golang with swig 》系列随笔)。
本文主要讲述C语言类型、在CGO中可以引用的C类型及对应的引用类型名称。
查询C99标准文档(6.2.5 Types),主要类型包括:_Bool、char、signed char、unsigned char、short、unsigned short、int、unsigned int、long、unsigned long、long long、unsigned long long、float、double、long double、float _Complex、double _Complex、long double _Complex、void *、__int128_t、__uint128_t
在CGO中引用C类型的名称可以参考CGO官方文档(Command cgo)。Go references to C 部分给出了可以引用的数值类型名称。
综合两份参考文献,给出下面的类型列表。
C Types |
Cgo Types |
_Bool |
C._Bool |
char |
C.char |
signed char |
C.schar |
unsigned char |
C.uchar |
short |
C.short |
unsigned short |
C.ushort |
int |
C.int |
unsigned int |
C.uint |
long |
C.long |
unsigned long |
C.ulong |
long long |
C.longlong |
unsigned long long |
C.ulonglong |
float |
C.float |
double |
C.double |
long double |
不支持(unexpected: 16-byte float type - long double) |
float _Complex |
C.complexfloat |
double _Complex |
C.complexdouble |
long double _Complex |
不支持( unexpected: 32-byte complex type - complex long double) |
void* |
unsafe.Pointer |
__int128_t |
[16]byte |
__uint128_t |
[16]byte |
C语言中的数值类型long double和long double _Complex在CGO中不能引用,编译报错。
写一个简单的程序来演示下:
package main /* #include <limits.h> #include <float.h> _Bool b = 1; char c = ‘A‘; signed char sc = CHAR_MIN; unsigned char usc = UCHAR_MAX; short s = SHRT_MIN; unsigned short us = USHRT_MAX; int i = INT_MIN; unsigned int ui = UINT_MAX; long l = LONG_MIN; unsigned long ul = ULONG_MAX; long long ll = LONG_LONG_MIN; unsigned long long ull = ULONG_LONG_MAX; float f = FLT_MIN; double d = DBL_MIN; long double ld = LDBL_MIN; float _Complex fc = 5+5i; double _Complex dc = 5+5i; long double _Complex ldc = 5+5i; __int128_t i128 = 0; __uint128_t ui128 = 3.4028236692093846346337460743177e+38; void *ptr = 0; */ import "C" import "fmt" import "unsafe" func main() { fmt.Println(C.b) fmt.Println(C.c) fmt.Println(C.sc) fmt.Println(C.usc) fmt.Println(C.s) fmt.Println(C.us) fmt.Println(C.i) fmt.Println(C.ui) fmt.Println(C.l) fmt.Println(C.ul) fmt.Println(C.ll) fmt.Println(C.ull) fmt.Println(C.f) fmt.Println(C.d) //fmt.Println(C.ld) fmt.Println(C.fc) fmt.Println(C.dc) //fmt.Println(C.ldc) fmt.Println(C.i128) fmt.Println(C.ui128) fmt.Println(C.ptr) fmt.Println("-------------------------------------------------") fmt.Println(C._Bool(true)) fmt.Println(C.char(‘A‘)) fmt.Println(C.schar(-128)) fmt.Println(C.uchar(255)) fmt.Println(C.short(C.SHRT_MIN)) fmt.Println(C.ushort(C.USHRT_MAX)) fmt.Println(C.int(C.INT_MIN)) fmt.Println(C.uint(C.UINT_MAX)) fmt.Println(C.long(C.LONG_MIN)) fmt.Println(C.ulong(C.ULONG_MAX)) fmt.Println(C.longlong(C.LONG_LONG_MIN)) fmt.Println(C.ulonglong(18446744073709551615)) fmt.Println(C.float(-1)) fmt.Println(C.double(-1)) //fmt.Println(C.longdouble(1)) fmt.Println(C.complexfloat(5 + 5i)) fmt.Println(C.complexdouble(5 + 5i)) C.i128 = [16]byte{15: 127} fmt.Println(C.i128) C.ui128 = [16]byte{15: 255} fmt.Println(C.ui128) C.ptr = unsafe.Pointer(nil) fmt.Println(C.ptr) }
运行后,输出结果如下:(运行环境:Windows 7 64位操作系统,go version go1.8.3 windows/amd64)
true 65 -128 255 -32768 65535 -2147483648 4294967295 -2147483648 4294967295 -9223372036854775808 18446744073709551615 1.1754944e-38 2.2250738585072014e-308 (5+5i) (5+5i) [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255] <nil> ------------------------------------------------- true 65 -128 255 -32768 65535 -2147483648 4294967295 -2147483648 4294967295 -9223372036854775808 18446744073709551615 -1 -1 (5+5i) (5+5i) [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255] <nil>
标签:sign cgo 实现 code double ++ highlight complex import
原文地址:http://www.cnblogs.com/majianguo/p/7650059.html