Hello-World CGO program

I’m sharing my first CGO program, I hope it helps.

File structure:

$ tree
.
├── hello.c
├── hello.h
└── main.go

hello.c

#include <stdio.h>

#include "hello.h"

void Greet() {
    printf("Hello, world!\n");
}

hello.h

#ifndef PLAYGROUND_HELLO_H
#define PLAYGROUND_HELLO_H

void Greet();

#endif //PLAYGROUND_HELLO_H

main.go

package main

// #include "hello.h"
import "C"

import (
	"fmt"
	"log"
)

func main() {
	if err := greet(); err != nil {
		log.Fatalf("Failed to call hello: %s", err)
	}
	log.Printf("Successfully called hello!\n");
}

func greet() error {
	_, err := C.Greet()
	if err != nil {
		return fmt.Errorf("call to hello failed: %s", err)
	}
	return nil
}

Build & run

$ go build -o main
$ ./main
Hello, world!
2021/04/26 14:30:39 Successfully called hello!

References

I followed Cgo: First steps tutorial, but the examples provided didn’t compile for me (see error down below), I had to create a header file (hello.h) and a source file (hello.c), as shown in this post.

/tmp/go-build070036073/b001/_x003.o: In function `printf':
/usr/include/x86_64-linux-gnu/bits/stdio2.h:104: multiple definition of `Greet'
/tmp/go-build070036073/b001/_x002.o:/usr/include/x86_64-linux-gnu/bits/stdio2.h:104: first defined here
collect2: error: ld returned 1 exit status

Update: Just realized that the tutorial referenced above explains how to fix the error. Anyway, now we have two slightly different ways to write a hello-world CGO program.


Posted

in

,

by

Tags: