Home > Backend Development > Golang > Is go language compatible with c language?

Is go language compatible with c language?

青灯夜游
Release: 2022-12-26 17:51:33
Original
5627 people have browsed it

Go language is compatible with c language; Go language can call C language and can also call C language. There is a Cgo command in the Go language toolkit, which is used to handle operations related to Go calling C; and Go functions can be exported to C, just add "//export funcname" in front of the function to be exported, and then you can Use the "go build -buildmode=c-shared -o libxxx.so" command to compile and generate dynamic libraries and header files for use in C language.

Is go language compatible with c language?

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

C language and golang are compatible. The .c file you write in C can be referenced by golang and used directly.

Many of the various modern high-level programming languages ​​are built on top of the C language. Basically, they can call the C language, and this is also required and exists in some cases. meaning. The Go language supports this aspect very well. It can not only call the C language, but also the C language. This article will explain the relevant content.

Basic description

There is a Cgo command in the Go language toolkit, which is used to handle operations related to Go calling C. We can use this command directly or call it automatically when running or building a Go program.

Cgo’s processing of C language itself relies on the C language-related compilation tool chain in the system, so you need to pay attention to the settings, mainly the Go environment variable settings:

Is go language compatible with c language?

Special attention should be paid to the fact that Cgo needs to be enabled, which can be set using the go env -w CGO_ENABLED=1 command. In the picture above, you can see some FLAGS parameters during C compilation. If necessary, you can adjust the settings accordingly. In addition, the above CC CXX is the setting of the compilation tool chain, which can also be adjusted according to the needs.

Calling C in Go

Calling C language in Go will eventually be displayed as a pseudo package named C in Go , In Go, use the comment #include ... above the import "C" line to reference C language-related libraries. The public variables and functions in these referenced libraries will be hung into the C package for use in Go. The following is a simple demonstration:

Is go language compatible with c language?

To call C in Go, just do the above, and it is very convenient to use.

Generally speaking, C language program projects may include assembly code in addition to C language code; or the project may also be C/C mixed programming. These projects can be used in Go, and Cgo will automatically recognize the suffix as .c .s .S .sx . cc .cpp .cxx file, and call the corresponding compiler to compile.

It should be noted that syntax not supported by C such as overloading and class methods in C needs to be wrapped with a layer of C language standard functions if you want to use it in Go. The usage method is the same as calling C in C language. Same. [Related recommendations: Go video tutorial, Programming teaching]

Calling Go in C

If there are any problems in the following test, you can try go clean and reset the project before proceeding.

Go functions can be exported to C. Just add //export funcname in front of the function to be exported, and then use go build -buildmode =c-shared -o libxxx.so The command compiles and generates dynamic libraries and header files for use in C language:

Is go language compatible with c language?
Is go language compatible with c language?

Also you can Use go build -buildmode=c-archive -o libxxx.a to compile and generate a static library that can be used in C language.

Differences in data types

The call between the two languages ​​​​is actually the transfer processing of data. It should be noted that the two are not the same after all. Language, so there are restrictions on the data types that can be interacted between the two. Sometimes there is a need to force data type conversion, such as the following code:

package rand

// #include <stdlib.h>
import "C"

func Random() int {
    return int(C.random()) // C函数返回值给Go,random的返回值是long类型
}

func Seed(i int) {
    C.srandom(C.uint(i)) // Go传值给C的函数,srandom函数接收uint类型数据
}
Copy after login

The basic numerical type conversions available between the two are as follows:

C.char,       C.schar (signed char),       C.uchar (unsigned char)
C.short,       C.ushort (unsigned short)
C.int,       C.uint (unsigned int)
C.long,       C.ulong (unsigned long)
C.longlong (long long),       C.ulonglong (unsigned long long)
C.float,       C.double
C.complexfloat (complex float),       C.complexdouble (complex double)
Copy after login

In addition to the above basic types, Pointers can also be passed around. In particular, the void* pointer in C language is equivalent to the unsafe.Pointer in Go.

__int128_t and __uint128_t in C are equivalent to [16]byte in Go.

C中函数传输参数为数组的话直接传递数组名就行,在Go中向这类函数传递数组需要传递数组第一个元素的地址,另外需要注意的是数组中元素也必须是C语言中支持的类型:

C.f(&C.arr[0])
Copy after login

C中并没有string类型,使用字符串时需要进行处理:

package print

// #include <stdio.h>
// #include <stdlib.h>
import "C"
import "unsafe"

func Print(s string) {
    cs := C.CString(s) // 这个方式会将字符串拷贝一份,返回指针,注意使用完需要释放内存
    defer C.free(unsafe.Pointer(cs)) // defer修饰的语句会在该函数退出前执行
    C.fputs(cs, (*C.FILE)(C.stdout))
}
Copy after login

另外C语言的字符串 *C.char 可以使用 C.GoString() 转换成Go中的字符串。

C中的 struct union enum 这些类型在Go使用需要加上对应的前缀,变成 struct_xxx union_xxx enum_xxx 。其中联合体在Go中将成为字节数组的形式。这些对象的成员名如果和Go的关键词一样的话,在Go中使用需要在成员名前面加下划线,比如 x._name

C中的 sizeof 在Go中需要使用 C.sizeof_T 方式使用,T是变量数据类型。

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of Is go language compatible with c language?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template