Open In App

How to use C++ in Go

Last Updated : 10 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Go (Golang) is known for its simplicity, efficiency, and concurrency model, while C++ offers high performance and control over system resources. There are cases where you might want/need to use the simpler interface of Go but with the performance of C++, for instance, when you have legacy C++ libraries or some specific functionality that is not available in Go. In this article, you will learn how to integrate C++ code into Go codebase in the easiest way possible.

Prerequisite

To follow this guide, you will need:

  • Go installed on your machine.
  • G++ (GNU C++ compiler) installed for compiling C++ code.
  • Basic understanding of both Go and C++ programming.
  • A working setup of cgo, which is part of Go.

Make sure your environment variables for Go (GOPATH) and g++ are properly set up.

Steps

Step 1: Create C++ Code

The process begins with the C++ code of what you want to include in your Go code. This C++ code you will build onto a shared library in order that Go can call it using cgo.

Create a file named example.cpp:

C++
// example.cpp
#include <iostream>

extern "C" {
    // A function that will be called from Go
    void helloFromCpp() {
        std::cout << "Hello from C++!" << std::endl;
    }
}

The extern "C" block ensures the C++ function can be accessed from Go without name mangling.

Step 2: Compile the C++ Code into a Shared Library

In order for Go to use the given C++ code, that code has to be compiled to a shared object file (. so or. dylib).

g++ -c -fPIC example.cpp -o example.o
g++ -shared -o libexample.so example.o

The -fPIC flag is used to generate position-independent code, and -shared creates a shared library.

Step 3: Write Go Code

Now, make a Go file of type main. go in which, integrate the shared H file that is also of C++ language. To use C functions in Go, we use the cgo package to operate and, with some additional reconfiguration, call C++ functions.

Go
// main.go
package main

/*
#cgo LDFLAGS: -L. -lexample
#include <stdio.h>

// Declare the C++ function from the shared library
extern void helloFromCpp();
*/
import "C"

func main() {
    // Call the C++ function
    C.helloFromCpp()
}

Step 4: Build and Run the Go Program

After writing the Go code, you can compile and run it. Ensure that the shared library (libexample.so) is in the same directory as your Go code.

go run main.go

Code

Here is a full example with both C++ and Go code:

C++ Code (example.cpp):

C++
#include <iostream>

extern "C" {
    void helloFromCpp() {
        std::cout << "Hello from C++!" << std::endl;
    }
}

Go Code (main.go):

Go
package main

/*
#cgo LDFLAGS: -L. -lexample
#include <stdio.h>

extern void helloFromCpp();
*/
import "C"

func main() {
    C.helloFromCpp()
}

Output

After running the Go program, the output will be:

Hello from C++!

This demonstrates that the Go code successfully called the C++ function and executed it.

Conclusion

The process of utilizing C++ code in a Golang application is the best method of combining the best functionalities of both. Expectedly, the cgo allows for the creation of C++ callbacks, which means that Go can interface with other C++ libraries or write high-performance functions. In this guide, I have described how to create C++ code, compile it into a shared library, and how to use cgo to call it in Go.


Next Article
Article Tags :

Similar Reads