Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use CGO in Go language

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use CGO in Go language, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

1. Go language calls C function example: the C header file referenced by package main / needs to be declared in the comment, followed by import "C", and there can be no space / # include # include # include void myprint (char* s) {printf ("% s\ n", s) between this line and the comment. } * / import "C" import ("fmt"unsafe") func main () {/ / strings created using C.CString need to be released manually. Cs: = C.CString ("Hello World\ n") C.myprint (cs) C.free (unsafe.Pointer (cs)) fmt.Println ("call C.sleep for 3s") C.sleep (3) return}

Run:

2. Go language calls C library functions:

Hello.c

# include void hello () {printf ("hello world\ n");}

Hello.h

# ifndef HELLO_H#define HELLO_H void hello (void); # endif

Compile:

Gcc-c hello.car-cru libhello.a hello.opackage main / / define the library path using # cgo / * # cgo CFLAGS:-I. # cgo LDFLAGS:-L. -lhello#include "hello.h" * / import "C" func main () {C.hello ()}

Run:

3. Export functions from Go language to C language:

Main.go

Package main / # include / / int add (int a, int b); / / import "C" import ("fmt") / / when using export, no other c functions can be defined in the same file, otherwise an error will be reported. / / use export to export functions to c language calls. / / export GoAddfunc GoAdd (a, b int) int {return a + b} func main () {a: = C.add (1mem2) fmt.Printf ("C.add (1mem2) return% d\ n", a)}

Cfunc.go

Package main / int GoAdd (int a, int b); / int add (int a, int b) / / {/ / return GoAdd (aMae b); / /} / / import "C"

Run:

4. Go language exports function pointers to c language for use:

There is another way to use this, which I use more often. Pass the function pointer, because the GO function cannot be addressed, so you need to write an intermediate function to do a conversion operation. The example is as follows:

Clibrary.c

# include # include "clibrary.h" / / parameters are function pointers void some_c_func (callback_fcn callback) {int arg = 2; printf ("C.some_c_func (): calling callback with arg =% d\ n", arg); int response = callback (2); printf ("C.some_c_func (): callback responded with% d\ n", response);}

Clibrary.h

# ifndef CLIBRARY_H#define CLIBRARY_H// defines function pointer typedef int (* callback_fcn) (int); void some_c_func (callback_fcn); # endif

Go code:

Package main / * # cgo CFLAGS:-I. # cgo LDFLAGS:-L. -lclibrary#include "clibrary.h" int callOnMeGo_cgo (int in) / / declare * / import "C" import ("fmt"unsafe") / / export callOnMeGofunc callOnMeGo (in int) int {return in + 1} func main () {fmt.Printf ("Go.main (): calling C function with callback to us\ n") / / convert C.some_c_func using unsafe.Pointer ((C.callback_fcn) (unsafe.Pointer (C.callOnMeGo_cgo)}

Intermediate function:

Package main / * # include int callOnMeGo (int); / / The gateway functionint callOnMeGo_cgo (int in) {printf ("C.callOnMeGo_cgo (): called with arg =% d\ n", in); / / call GO function return callOnMeGo (in);} * / import "C"

Run:

Development considerations:

1. There can be no blank lines between comments and import "C"

two。 Manually release the string when converting GoString to CString using the C.CString function.

3. CGO does not support functions with variable parameters, such as printf. If you want to use it, you can write a wrapper function m'yprintf and call it by passing parameters.

4. Go supports using / / export to export functions to C for use, but it should be noted that c functions cannot be defined in the same file exported by export, otherwise it will appear

Multiple definition of "xxx" compilation error, if the function is very tiny, another way is to use static inline to declare the function, as follows:

Package gocallback import ("fmt"sync") / * extern void go_callback_int (int foo, int p1); / / normally you will have to define function or variables// in another separate C file to avoid the multiple definition// errors, however, using "static inline" is a nice workaround// for simple functions like this one.static inline void CallMyFunction (int foo) {go_callback_int (foo, 5) } * / import "C" Thank you for reading this article carefully. I hope the article "how to use CGO in Go language" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report