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 do golang and c language call each other

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

Share

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

This article mainly shows you "how golang and c language call each other", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how golang and c language call each other" this article

Go language calls c language

The following is a short example:

Package main// # include / / # include / * void print (char * str) {printf ("% s\ n", str);} * / import "C" import "unsafe" func main () {s: = "Hello Cgo" cs: = C.CString (s) C.print (cs) C.free (unsafe.pointer (cs)}

Compared with the "normal" go code, the above code has several "special" features:

The word include of the c language header file appears in the opening comment

The c language function print is defined in the comment

Import has a "package" called C

The c language function print defined above is called in the main function.

First of all, the c language code in the go source file needs to be wrapped with comments, just like the above include header file and the print function definition; second, the import "C" statement is necessary, and it and the above c code cannot be separated by blank lines, but must be closely linked. The "C" here is not a package name, but a concept similar to a namespace, or can be understood as a pseudo-package under which all the syntax elements of the c language are under; finally, access to the c syntax element is preceded by a pseudo-package prefix, such as C.uint and C.print, C.free in the above code.

For more details on the use of calling C language in go, please refer to the interoperation between Go and C language, which will not be described in detail in this article.

In the above example, the c language is embedded in go code, which is obviously unprofessional if the code is larger and more complex. So, can the c language code be separated from the go code and defined separately? The answer is yes, which can be achieved by sharing libraries.

Cgo provides the # cgo indicator to specify which shared libraries the go source code links with after compilation. Examples are as follows:

/ / hello.gopackage main// # cgo LDFLAGS:-L. /-lhello// # include / / # include / / # include "hello.h" import "C" func main () {C.hello ()} / / hello.c#include "hello.h" void hello () {printf ("hello, go\ n");} / / hello.hextern void hello ()

In hello.go, add LDFLAGS:-L. /-lhello after the # cgo indicator to specify that the so library be found and linked in the current directory when the go code is compiled.

Therefore, you only need to compile hello.c into a dynamic library, and then compile the go code, and you can call the c language functions in the shared library when you run the go code. The instructions are as follows:

Gcc-fPIC-o libhello.so hello.c

Go build-o hello

. / hello

C language calls go language

Compared with calling the c source code in go, there are fewer occasions to use the go function in c. Because generally speaking, using a high-level language as a glue to call a low-level language can give full play to their respective characteristics, while calling a high-level language with a low-level language may reduce the performance advantage of the low-level language. In go, you can use "export + function name" to export the go function for c code. See a simple example:

/ / hello.gopackage mainimport "C" import "fmt" / / export Go2Cfunc Go2C () {fmt.Println ("hello, C")}

Through the compilation option of go build, go code can be compiled into a shared library for call by c code. Note that the main and main functions must exist when compiling the so library (even if the main function is empty). The compilation instructions are as follows: go build-v-x-buildmode=c-shared-o libhello.so hello.go.

After compiling successfully, you only need to introduce the newly generated header file into the c code and link the dynamic library at compile time to realize the call of the go function. The code is as follows:

/ / hello.c#include # include "libhello.h" int main () {Go2C (); return 0;}

Through gcc-o hello-L. -lhello, can be compiled into an executable program. Note that before running, you must make sure that there are shared libraries that need to be linked in the runtime lookup path of the shared library, either by placing the so library path to / usr/lib or by modifying the environment variable LD_LIBRARY_PATH.

The above is all the contents of the article "how golang and c are called each other". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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